1

使用以下代码,我的后台工作人员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# 重写代码时,我遇到了这个问题。

4

1 回答 1

9

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

 backgroundWorker1.DoWork += new DoWorkEventHandler(backgroundWorker1_DoWork);
 backgroundWorker1.RunWorkerCompleted += new RunWorkerCompletedEventHandler(
                                                 backgroundWorker1_RunWorkerCompleted);
于 2013-07-16T19:39:50.850 回答