[英]error handling with BackgroundWorker
I know, that you can handle BackgroundWorker errors in RunWorkerCompleted handler, like in next code 我知道,您可以在RunWorkerCompleted处理程序中处理BackgroundWorker错误,就像在下一个代码中一样
var worker = new BackgroundWorker();
worker.DoWork += (sender, e) =>
{
throw new InvalidOperationException("oh shiznit!");
};
worker.RunWorkerCompleted += (sender, e) =>
{
if(e.Error != null)
{
MessageBox.Show("There was an error! " + e.Error.ToString());
}
};
worker.RunWorkerAsync();
But my problem is that i still receive a message : error was unhadled in user code on line 但我的问题是我仍然收到一条消息:错误已在用户代码中排除在线
throw new InvalidOperationException("oh shiznit!");
How can i resolve this problem ? 我该如何解决这个问题?
You receive it because you have a debugger attached. 您收到它是因为您附加了调试器。 Try to start the application without a debugger: no exception is fired and when the worker completes the operation show you the MessageBox.
尝试在没有调试器的情况下启动应用程序:不会触发异常,并且当worker完成操作时会显示MessageBox。
I cannot reproduce the error. 我无法重现错误。 The following works fine:
以下工作正常:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
var worker = new BackgroundWorker();
worker.DoWork += (s, evt) =>
{
throw new InvalidOperationException("oops");
};
worker.RunWorkerCompleted += (s, evt) =>
{
if (evt.Error != null)
{
MessageBox.Show(evt.Error.Message);
}
};
worker.RunWorkerAsync();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.