BackgroundWorker的异常处理 [英] BackgroundWorker exception handling

查看:225
本文介绍了BackgroundWorker的异常处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下部件工作:




  • 图书馆(其中抛出异常)

  • 测试的控制台来测试我的日志

  • 企业库的异常处理应用程序块

  • 企业库日志应用程序块



我用一个BackgroundWorker调用库方法。图书馆引发异常,但RunWorkerCompleted处理程序不会被调用。



要捕捉异常的唯一方法就是围绕一个try / catch块我的DoWork的处理程序代码。



所做的是误解了RunWorkerCompletedEventArgs.Error房产吗? ?是不是为获得这得到了由BackgroundWorker的捕获的异常



Codesample:

 静态BackgroundWorker的W =新的BackgroundWorker(); 

w.DoWork + =新DoWorkEventHandler(w_DoWork);
w.RunWorkerCompleted + =
新RunWorkerCompletedEventHandler(w_RunWorkerCompleted);
w.RunWorkerAsync();



静态无效w_DoWork(对象发件人,DoWorkEventArgs E)
{
MyClass的M =新MyClass的();
w.result = m.Compute();
}

静态无效w_RunWorkerCompleted(对象发件人,RunWorkerCompletedEventArgs E)
{
如果(e.Error!= NULL)
{
HandleException(e.Error);
}

/ *结果相关的代码* /

}


静态无效HandleException(例外五)
{
ExceptionPolicy.HandleException(即MyPolicy);
}

上面的示例导致了我的控制台应用程序的终止。 Visual Studio 2010的输出写入绝对没有(仅默认输出)。



那么,是什么问题?



//编辑:这段代码适用于捕捉库的异常

 静态无效w_DoWork(对象发件人,DoWorkEventArgs E)
{

{
MyClass的M =新MyClass的();
w.result = m.Compute();
}赶上(例外五){}

}


解决方案

这是BackgroundWorker的正确模式。



我怀疑的问题是,你的的BW完成之前方法退出。



的RunWorkerAsync 将立即返回,如果你不守候在,那么你的过程将结束,在BW已经开始甚至之前,没关系完成。



尝试添加到Console.ReadLine 你的方法的末尾。






出于兴趣:



BW表现不同的控制台应用程序和一个Windows应用程序。如果你使用一个WinForms或WPF应用程序,就会出现在你的UI线程派生的SynchronizationContext和BW将元帅 RunWorkerCompleted 回UI线程并运行它。这就是BW的主要好处之一。



在一个控制台应用程序,默认的SynchronizationContext使用,这乘警 RunWorkerCompleted 到一个线程池线程。这意味着你可以阻止线程和完成处理程序将继续运行。


I'm working with the following components:

  • a Library (which throws an exception)
  • a test-console to test my logging
  • the enterprise library exception handling application blocks
  • the enterprise library logging application blocks

I'm invoking the library method by using a backgroundworker. The library throws the exception but the RunWorkerCompleted handler is never called.

The only way to catch the exception is to surround my DoWork handler code with a try/catch block.

Did is misunderstand the RunWorkerCompletedEventArgs.Error Property? Isn't it for getting exceptions which got caught by the BackgroundWorker?

Codesample:

static BackgroundWorker w = new BackgroundWorker();

w.DoWork += new DoWorkEventHandler(w_DoWork);
w.RunWorkerCompleted += 
   new RunWorkerCompletedEventHandler(w_RunWorkerCompleted);
w.RunWorkerAsync();



static void w_DoWork(object sender, DoWorkEventArgs e)
{
   MyClass m  = new MyClass();
   w.result = m.Compute();
}

static void w_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
   if (e.Error != null)
   {
      HandleException(e.Error);
   }

   /* result related code */

}


static void HandleException(Exception e)
{
   ExceptionPolicy.HandleException(e, "MyPolicy");
}

The above sample leads to a termination of my console application. The vs2010 output writes absolutely nothing (only default output).

So where's the problem?

//Edit: this snippet works for catching the library's exception.

static void w_DoWork(object sender, DoWorkEventArgs e)
{
   try
   {
      MyClass m  = new MyClass();
      w.result = m.Compute();
   }catch(Exception e){ }

}

解决方案

That is the correct pattern for BackgroundWorker.

I suspect the problem is that your Main method is exiting before the BW has completed.

RunWorkerAsync will return immediately and if you are not waiting in Main, then your process will end, perhaps even before the BW has started, never mind completed.

Try adding a Console.ReadLine at the end of your Main method.


Out of interest:

BW behaves differently in a Console app and a Windows app. If you use a WinForms or WPF app, there will be a derived SynchronizationContext on your UI thread and BW will marshal RunWorkerCompleted back to the UI thread and run it there. That's one of the main benefits of BW.

In a Console App, the default SynchronizationContext is used and this marshals RunWorkerCompleted onto a thread pool thread. This means you can block the Main thread and the completed handler will still run.

这篇关于BackgroundWorker的异常处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆