使用Task.Factory追赶时出错Task、Factory

2023-09-03 03:34:23 作者:谁能真心疼涐

我用下面的

  Task.Factory.StartNew(()=> DoPrintConfigPage(串行));
 

那么函数我打电话看起来像这样

 私人无效DoPrintConfigPage(字符串序列)
{
    //做印刷工作
}
 

我的问题是一个异常被抛出的线程内,而不是被处理。

我已经试过包装在一个尝试捕捉

 尝试
{
    Task.Factory.StartNew(()=> DoPrintConfigPage(串行));
}
赶上(例外前){}
 
labview软件生成的时候出错

但仍无法追赶的错误,因此崩溃应用程序。

如何能赶上在主线程中的异常,所以我可以处理?

更新

我已经推荐下面的变化,仍然跟它的例外是未处理

  VAR任务= Task.Factory.StartNew(()=> DoPrintConfigPage(串行))
                               .ContinueWith(TSK =>
                               {
                                  的MessageBox.show(破事);
                               },TaskContinuationOptions.OnlyOnFaulted);
 

然后在我的 DoConfigPage 我添加了另一个尝试捕捉。

在此美中不足的是,现在它崩溃,说的异常被抛出了未处理的,我缺少什么?

 私人无效DoPrintConfigPage(字符串序列)
{
    尝试
    {
        //调用打印功能
    }
    赶上(例外前)
    {
        抛出前; //它崩溃在这里,并说这是未处理
    }
}
 

我也试了一下埃里克J.建议用相同的结果。

  VAR任务= Task.Factory.StartNew(()=> DoPrintConfigPage(串行));

尝试
{
    task.Wait();
}
赶上(AggregateException前){的MessageBox.show(破事); }
 

解决方案

另外,您也可以链接你的任务创建并添加ContinueWith:

  VAR工作= Task.Factory
    .StartNew(...)
    .ContinueWith(TSK =>
         {
              //检查啧异常和处理
         });
 

编辑:这段代码在运行时,会弹出消息框对我来说:

 无效的主要()
{
    VAR系列=某些串行;
    VAR任务= Task.Factory
        .StartNew(()=> DoPrintConfigPage(串行))
        .ContinueWith(TSK =>
        {
            的MessageBox.show(破事);
            变种扁平= tsk.Exception.Flatten();

            //注意:不要实际上处理异常这样一来,m'kay?
            flattened.Handle(EX => {的MessageBox.show(错误:+ ex.Message);返回true;});
        },TaskContinuationOptions.OnlyOnFaulted);

}

公共无效DoPrintConfigPage(字符串序列)
{
    抛出新的异常(嘭!);
}
 

i am using the following

Task.Factory.StartNew(() => DoPrintConfigPage(serial));

then the function i am calling looks like this

private void DoPrintConfigPage(string serial) 
{ 
    //do printing work 
}

My problem is an exception is being thrown inside the thread and not being handled.

I have tried wrapping it in a try catch

try
{
    Task.Factory.StartNew(() => DoPrintConfigPage(serial));
}
catch (Exception ex) { }

but it still is not catching the error and thus crashing the application.

How can I catch exceptions in the main thread so I can handle them?

Update

I have made the changes recommended below and still it is saying the exception is unhandled

var task =  Task.Factory.StartNew(() => DoPrintConfigPage(serial))
                               .ContinueWith(tsk =>
                               {
                                  MessageBox.Show("something broke");
                               },TaskContinuationOptions.OnlyOnFaulted);

then in my DoConfigPage I added another try catch.

In this catch is now where it is crashing and saying the exception being thrown was unhandled, what am I missing?

private void DoPrintConfigPage(string serial)
{
    try
    {
        //call the print function
    }
    catch (Exception ex)
    {
        throw ex;   //it is crashing here and saying it is unhandled
    }
}

I also tried what Eric J. suggested with the same results

var task = Task.Factory.StartNew(() => DoPrintConfigPage(serial));

try
{
    task.Wait();                  
}
catch (AggregateException ex) { MessageBox.Show("something broke"); }

解决方案

Alternatively, you can chain your task creation and add a ContinueWith:

var job = Task.Factory
    .StartNew(...)
    .ContinueWith(tsk => 
         {
              // check tsk for exception and handle
         });

EDIT: This snippet, when run, pops up the message box for me:

void Main()
{
    var serial = "some serial";
    var task =  Task.Factory
        .StartNew(() => DoPrintConfigPage(serial))
        .ContinueWith(tsk =>
        {
            MessageBox.Show("something broke");
            var flattened = tsk.Exception.Flatten();

            // NOTE: Don't actually handle exceptions this way, m'kay?
            flattened.Handle(ex => { MessageBox.Show("Error:" + ex.Message); return true;});
        },TaskContinuationOptions.OnlyOnFaulted);

}

public void DoPrintConfigPage(string serial)
{
    throw new Exception("BOOM!");
}

 
精彩推荐
图片推荐