【发布时间】:2011-05-07 13:43:40
【问题描述】:
在主线程中我有一个Timer。在Tick 事件中,我运行BackgroundWorker。我在那里做了一些事情,然后BackgroundWorker 调用RunWorkerCompleted 事件。
在主线程中我有函数Stop。此函数禁用Timer。但是我想等他工作的时候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(); 完成。
谢谢
【问题讨论】:
-
没有。您无需等待后台工作人员..这就是为什么它被称为后台工作人员-您的应用程序在后台工作人员工作时继续做事。您将一个事件侦听器附加到您的应用程序,该事件侦听器应在后台工作人员完成后触发,然后处理数据。
-
Private Sub backgroundWorker1_RunWorkerCompleted(_ByVal sender As Object, ByVal e As RunWorkerCompletedEventArgs) _ Handles backgroundWorker1.RunWorkerCompleted
标签: c# .net multithreading backgroundworker