Search code examples
c#winformsbackgroundworker

Background worker RunWorkerCompleted is never fired


Using the following code my background worker RunWorkerCompleted is never called and I can't figure out why.

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();
    }
}

My goal is for the background worker to run continuously, I had this working in .NET but when I rewrote the code now in C# I'm having this issue.


Solution

  • Do you have all the events hooked up correctly ??

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