在主线程中我有一个计时器
。在 Tick
事件中,我运行一个 BackgroundWorker
。我在那里做了一些事情,然后 BackgroundWorker
调用 RunWorkerCompleted
事件。
在主线程中,我有函数Stop
。此函数禁用定时器
。但我想等 BackgroundWorker
当他工作时。
例如:
public void Next()
{
// Start the asynchronous operation
if (!this._backgroundWorker.IsBusy)
this._backgroundWorker.RunWorkerAsync();
}
private void _backgroundWorker_DoWork(object sender, DoWorkEventArgs e)
{
DoSomething();
}
private void _backgroundWorker_RunWorkerCompleted(object sender,
RunWorkerCompletedEventArgs e)
{
DoSomethingElse();
}
public void Stop()
{
this._timer.Enabled = false;
}
所以我的问题是如何等待BackgroundWorker
的RunWorkerCompleted
事件?我需要等到 DoSomethingElse();
完成。
谢谢
最佳答案
处理BackgroundWorker.RunWorkerCompleted当后台操作完成、取消或引发异常时发生的事件。
// This event handler deals with the results of the
// background operation.
private void backgroundWorker1_RunWorkerCompleted(
object sender, RunWorkerCompletedEventArgs e)
{
// First, handle the case where an exception was thrown.
if (e.Error != null)
{
}
else if (e.Cancelled)
{
// Next, handle the case where the user canceled
// the operation.
// Note that due to a race condition in
// the DoWork event handler, the Cancelled
// flag may not have been set, even though
// CancelAsync was called.
}
else
{
// Finally, handle the case where the operation
// succeeded.
}
}
关于c# - 等待BackgroundWorker RunWorkerCompleted,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5921437/