How To Start And Stop A Continuously Running Background Worker Using A Button

Maybe you can use a manualresetevent like this, I didn’t debug this but worth a shot. If it works you won’t be having the thread spin its wheels while it’s waiting

ManualResetEvent run = new ManualResetEvent(true);

private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e) 
{ 
     while(run.WaitOne()) 
     { 
         //Kill zombies 
     } 
} 

private void War() 
{ 
    run.Set();
} 

private void Peace() 
{ 
    run.Reset();
}

Leave a Comment