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

查看:126
本文介绍了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 的属性。如果您在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天全站免登陆