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

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

问题描述

我的 WinForms 应用程序使用了许多 BackgroundWorker对象从数据库中检索信息.我使用 BackgroundWorker 是因为它允许 UI 在长时间运行的数据库查询期间保持畅通无阻,并且为我简化了线程模型.

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?

推荐答案

如果操作引发了您的代码无法处理的异常,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.

http://msdn.microsoft.com/en-us/library/system.componentmodel.backgroundworker.dowork.aspx

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

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