在BackgroundWorker的未处理的异常 [英] Unhandled exceptions in BackgroundWorker

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

问题描述

我的WinForms应用程序使用了大量的<一href="http://msdn.microsoft.com/en-us/library/system.componentmodel.backgroundworker.aspx">BackgroundWorker对象从数据库检索信息。我使用BackgroundWorker的,因为它允许用户界面保持畅通,在长时间运行的数据库查询,它简化了线程模型适合我。

My WinForms app uses a number of BackgroundWorker objects to retrieve information from a database. I'm using BackgroundWorker because it allows the UI to remain unblocked during long-running database queries and it simplifies the threading model for me.

我得到的一些后台线程偶尔DatabaseExceptions,我亲眼目睹至少有一种例外的工作线程同时调试。我相当有信心,这些例外的是,我想超时的合理期望,不时。

I'm getting occasional DatabaseExceptions in some of these background threads, and I have witnessed at least one of these exceptions in a worker thread while debugging. I'm fairly confident these exceptions are timeouts which I suppose its reasonable to expect from time to time.

我的问题是什么时候未处理的异常在这些后台工作线程之一时会发生什么。

My question is about what happens when an unhandled exception occurs in one of these background worker threads.

我不认为我能赶上在另一个线程的异常,但是我可以期待将要执行我的WorkerCompleted方法?有BackgroundWorker的任何属性或方法我可以询问有关例外?

I don't think I can catch an exception in another thread, but can I expect my WorkerCompleted method to be executed? Is there any property or method of the BackgroundWorker I can interrogate for exceptions?

推荐答案

如果该操作会引发异常,你的code不处理,则的BackgroundWorker 捕捉异常并将其传递到 RunWorkerCompleted 事件处理程序,它是公开为 System.ComponentModel.RunWorkerCompletedEventArgs 的Error属性。如果您在Visual Studio调试运行,调试器将在其中未处理的异常升高的DoWork的事件处理程序的点突破。

If the operation raises an exception that your code does not handle, the BackgroundWorker catches the exception and passes it into the RunWorkerCompleted event handler, where it is exposed as the Error property of System.ComponentModel.RunWorkerCompletedEventArgs. If you are running under the Visual Studio debugger, the debugger will break at the point in the DoWork event handler where the unhandled exception was raised.

<一个href="http://msdn.microsoft.com/en-us/library/system.componentmodel.backgroundworker.dowork.aspx">http://msdn.microsoft.com/en-us/library/system.componentmodel.backgroundworker.dowork.aspx

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

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