从另一个线程捕获 Application.ThreadException 中的错误
我有一个来自应用程序的窗口,其program.cs代码如下:
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.ThreadException += (sndr, evnt) =>
{
MessageBox.Show("An error occur. Please contact your administrator.\nError description: " +
evnt.Exception.Message + "\nStack trace:\n" + evnt.Exception.StackTrace,
evnt.Exception.GetType().ToString(), MessageBoxButtons.OK, MessageBoxIcon.Error);
};
Application.Run(new frmLogin());
}
}
但是当在 BackgroundWorker.DoWork
事件中引发异常时,Application.ThreadException
没有捕获它。
如何 Application.ThreadException
捕获由 BackgroundWorker
创建的不同线程的异常?< /strong>
我知道在 Application.ThreadException
中捕获错误不是一个好的做法,但我们仅将其用作最后一级的错误捕获。
我的代码是用 C# 编写的,框架4,在VS2010 Pro中构建。
请帮忙。提前致谢。
I have a windows from application with a program.cs code like this:
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.ThreadException += (sndr, evnt) =>
{
MessageBox.Show("An error occur. Please contact your administrator.\nError description: " +
evnt.Exception.Message + "\nStack trace:\n" + evnt.Exception.StackTrace,
evnt.Exception.GetType().ToString(), MessageBoxButtons.OK, MessageBoxIcon.Error);
};
Application.Run(new frmLogin());
}
}
But when an exception is thrown inside the BackgroundWorker.DoWork
event, the Application.ThreadException
didn't catch it.
How can the Application.ThreadException
catch an exception from a different thread created by a BackgroundWorker
?
I know catching errors in the Application.ThreadException
is not a good practice but we only use it as our last level of error catching.
My code is in C#, framework 4, build in VS2010 Pro.
Please help. Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
请参阅此问题:BackgroundWorker 异常不会像这样传播,因此您无法捕获它们在 ThreadException 中。
抛出异常的BackgroundWorkers照常调用
RunWorkerCompleted
,但在事件参数上设置Error
属性。See this question: BackgroundWorker exceptions don't get propagated like that, so you can't catch them in
ThreadException
.BackgroundWorkers that throw exceptions call
RunWorkerCompleted
as usual, but set theError
property on the event arguments.BackgroundWorker 为您捕获异常(不会暴露给 Application.ThreadException 或 AppDomain.UnhandledException)并将异常传递给 RunWorkerCompleted 事件的参数。您将在 RunWorkerCompleted 事件处理程序上处理异常。
一旦 RunWorkerCompleted 事件触发,您可以得到 RunWorkerCompletedEventArgs.Error 异常。
The BackgroundWorker catches exception for you(will not expose to Application.ThreadException or AppDomain.UnhandledException) and pass the exception to argument of RunWorkerCompleted event. You will handle the exception on RunWorkerCompleted event handler.
There is RunWorkerCompletedEventArgs.Error that you can get the exception once the RunWorkerCompleted event fired.