wpf Backgroundworker 的 RunWorkerCompleted 事件在完成工作之前就已触发
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17925519/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Backgroundworker's RunWorkerCompleted event is firing even before completing the work
提问by Vikram
I am using the BackgroundWorker thread to perform a long task(basically reading a big xml file). For the first time the worker works fine as desired, but if I upload a second xml file, using the same background worker it works fine sometimes but most of the time the Backgroundworker's RunWorkerCompleted is fired even before the DoWork event. Some of the code is displayed below
我正在使用 BackgroundWorker 线程来执行一个很长的任务(基本上是读取一个大的 xml 文件)。第一次工作人员按需要正常工作,但如果我上传第二个 xml 文件,使用相同的后台工作人员有时工作正常,但大多数情况下,后台工作人员的 RunWorkerCompleted 甚至在 DoWork 事件之前就被触发。部分代码显示如下
private void openFile_Click(object sender, RoutedEventArgs e)
{
// Code removed for brevity
worker = new BackgroundWorker();
worker.RunWorkerAsync();
worker.DoWork += new DoWorkEventHandler(worker_DoWork);
worker.WorkerReportsProgress = true;
worker.ProgressChanged += new ProgressChangedEventHandler(worker_ProgressChanged);
worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(worker_RunWorkerCompleted);
}
void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
DataImport();
//worker.Dispose();
//worker.Disposed += new EventHandler(worker_Disposed);
//worker.DoWork -= worker_DoWork;
//worker.RunWorkerCompleted -= worker_RunWorkerCompleted;
//worker = null;
//GC.Collect(GC.GetGeneration(worker), GCCollectionMode.Forced);
}
worker is a globally defined variable. What is wrong here I am not getting. Kindly help
worker 是一个全局定义的变量。这里有什么问题我没有得到。请帮忙
回答by sloth
You should add the DoWork-event handler (and all other event handler, too) beforecalling RunWorkerAsync().
您应该在调用之前添加DoWork-event 处理程序(以及所有其他事件处理程序)。RunWorkerAsync()
Otherwise, it could happen that RunWorkerAsyncdoes practically nothing.
否则,它可能会发生RunWorkerAsync几乎没有任何作用。
回答by Vishal
It should be like this:
应该是这样的:
worker = new BackgroundWorker();
worker.WorkerReportsProgress = true;
worker.DoWork += new DoWorkEventHandler(worker_DoWork);
worker.ProgressChanged +=
new ProgressChangedEventHandler(worker_ProgressChanged);
worker.RunWorkerCompleted +=
new RunWorkerCompletedEventHandler(worker_RunWorkerCompleted);
worker.RunWorkerAsync();
RunWorkerAsyncshould be called after subscribing to the DoWorkand RunWokerCompletedevents.
RunWorkerAsync应在订阅DoWork和RunWokerCompleted事件后调用。
回答by Mukesh Kumar
you should check first that background worker is busy or not, using this....
你应该首先检查后台工作人员是否忙,使用这个......
backgroundWorker1.DoWork += backgroundWorker1_DoWork; backgroundWorker1.RunWorkerCompleted += backgroundWorker1_RunWorkerCompleted;
backgroundWorker1.DoWork += backgroundWorker1_DoWork; backgroundWorker1.RunWorkerCompleted += backgroundWorker1_RunWorkerCompleted;
if (backgroundWorker1.IsBusy)
{
backgroundWorker1.CancelAsync();
}
else
{
backgroundWorker1.RunWorkerAsync();
}
回答by Eduardo Arruda
As said in https://stackoverflow.com/a/16809596/8029935, you can be blindly assuming that the worker has finished the job and produced a result when DoWork Method dies from an exception. Which is caught by BackgroundWorker and passed to the RunWorkerCompleted event handler as the e.Error property.
正如https://stackoverflow.com/a/16809596/8029935 中所说,当 DoWork Method 因异常而死亡时,您可以盲目地假设工作人员已经完成了工作并产生了结果。它被 BackgroundWorker 捕获并作为 e.Error 属性传递给 RunWorkerCompleted 事件处理程序。
So, I suggest you must check that property using a try/catch statement.
因此,我建议您必须使用 try/catch 语句检查该属性。

