后台工作程序RunWorkerCompleted永远不会被触发

时间:2013-07-16 19:35:00

标签: c# winforms backgroundworker

使用以下代码,我的后台工作人员RunWorkerCompleted永远不会被调用,我无法找出原因。

void startWaitScan()
{
    backgroundWorker1.RunWorkerAsync();
}

void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
    // do something here
}

void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
    Console.WriteLine("BackgroundWorker1_RunWorkerCompleted");
    if (!stopAsync)
    {
        backgroundWorker1.RunWorkerAsync();
    }
}

我的目标是让后台工作者连续运行,我在.NET中工作但是当我用C#重写代码时,我遇到了这个问题。

1 个答案:

答案 0 :(得分:9)

您是否正确连接了所有事件?

 backgroundWorker1.DoWork += new DoWorkEventHandler(backgroundWorker1_DoWork);
 backgroundWorker1.RunWorkerCompleted += new RunWorkerCompletedEventHandler(
                                                 backgroundWorker1_RunWorkerCompleted);