Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Restart background worker

Is there a way to directly "restart" a background worker? Calling CancelAsync() followed by RunWorkerAsync() clearly won't do it as their names imply.

Background info: I have a background worker which calculates a total in my .net 2.0 Windows Forms app. Whenever the user modifies any value which is part of this total I'd like to restart the background worker in case it would be running so that directly the latest values are considered.

like image 871
Marc Avatar asked Jun 17 '09 06:06

Marc


2 Answers

The backgriound work itself does not do any cancleing.

When you call bgw.CancelAsync it sets a flag on the background worker that you need to check yourself in the DoWork handler.

something like:

bool _restart = false;

private void button1_Click(object sender, EventArgs e)
{
    bgw.CancelAsync();
    _restart = true;
}

private void bgw_DoWork(object sender, DoWorkEventArgs e)
{

    for (int i = 0; i < 300; i++)
    {
        if (bgw.CancellationPending)
        {
            break;
        }
        //time consuming calculation
    }
}

private void bgw_WorkComplete(object sender, eventargs e)  //no ide to hand not sure on name/args
{
    if (_restart)
    {
        bgw.RunWorkerAsync();
        _restart = false;
    }

}
like image 177
Pondidum Avatar answered Sep 29 '22 08:09

Pondidum


There are a couple of options, it all depends on how you want to skin this cat:

If you want to continue to use BackgroundWorker, then you need to respect the model that has been established, that is, one of "progress sensitivity". The stuff inside DoWork is clearly required to always be aware of whether or not the a pending cancellation is due (i.e., there needs to be a certain amount of polling taking place in your DoWork loop).

If your calculation code is monolithic and you don't want to mess with it, then don't use BackgroundWorker, but rather fire up your own thread--this way you can forcefully kill it if needs be.

like image 22
Eric Smith Avatar answered Sep 29 '22 07:09

Eric Smith