views:

519

answers:

3

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.

+1  A: 

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

}
Pondidum
I'm very much aware of this. The question is about whether there is any other mechanism that allows to directly (force?) quit and restart the background worker.
Marc
That is not what your question implied. And no there isn't, As Eric said, you will just have to use a Thread and abort that manually.
Pondidum
A: 

You can hook the change event handler for the controls in which the values are changed and do the following in the handler:

if(!bgWrkr.IsBusy)
     //start worker
else if(!bgWrkr.CancellationPending)
  bgWrkr.CancelAsync();

Hope it helps you!

TheVillageIdiot
This doesn't solve the problem but just outlines it: If I call CancelAsync() in your example then the background worker won't be restarted to re-calculate with the latest values. Calling RunWorkerAsync() after CancelAsync() also won't fix the problem as the cancellation is handled asynchronosly...
Marc
+2  A: 

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.

Eric Smith
OK that sounds interesting.Can you recommend any specific documentation for using threads directly?
Marc
Check this MSDN link: http://msdn.microsoft.com/en-us/library/system.threading.thread.aspx
Pondidum