How to "kill" background worker completely?

Posted by Ken Hung on Stack Overflow See other posts from Stack Overflow or by Ken Hung
Published on 2009-04-29T03:37:19Z Indexed on 2010/04/07 3:13 UTC
Read the original article Hit count: 397

Filed under:
|

Hi All,

I am writing a windows application that runs a sequence of digital IO actions repeatedly.

This sequence of actions starts when the user click a "START" button, and it is done by a background worker in backgroundWorker1_DoWork().

However, there are occasions when I get the "This backgroundworker is currently busy......." error message.

I am thinking of implementing the following in the code, by using a while loop to "kill" the background worker before starting another sequence of action:

if (backgroundWorker1.IsBusy == true)
{

    backgroundWorker1.CancelAsync();
    while (backgroundWorker1.IsBusy == true)
    {
        backgroundWorker1.CancelAsync();
    }

    backgroundWorker1.Dispose();

}

backgroundWorker1.RunWorkerAsync();

I think my main concern is, will the backgroundWorker1 be "killed" eventually? If it will, will it take a long time to complete it?

Will this coding get me into an infinite loop?

© Stack Overflow or respective owner

Related posts about backgroundworker

Related posts about c#