Question

Say I'm showing the user a form, and using a BackgroundWorker to do some work behind the scenes.

When the user clicks OK, I cannot continue until the BackgroundWorker has completed. If it hasn't finished when the user clicks Ok, I want to show a WaitCursor until it has, and then continue.

What's the best way to implement this?

I know I could use a plain old Thread, and then do Thread.Join, but I like BackgroundWorker.

Can it be done nicely?

Was it helpful?

Solution

You could use this code, instead of BW

var resetEvent = new ManualResetEvent(false);
ThreadPool.QueueUserWorkItem(state =>
                {
                    Thread.Sleep(5000);//LongRunning task, async call
                    resetEvent.Set();
                });
resetEvent.WaitOne();// blocking call, when user clicks OK 

OTHER TIPS

Ideally, you should disable all the relevant controls on the form, then re-enable them when the BackgroundWorker completes. That way you won't get a locked UI - and you can have a cancel button etc.

If you wanted to actually block until it had finished, you could just run the task synchronously to start with.

You could check is the BackgroundWorker is running with a check in the btnOK_Click event handler to see if bw.IsBusy is true. If so change the cursor and set a boolean flag that indicates that OK handler is waiting. When BackgroundWorker finishes its job check if ok had been waiting, if so, set the flag to false change the cursor and do the job of the OK button. :-)

I'd just use boolean flags and a lock.

object waitlock = new object();
bool isBackgroundWorkerCompleted = false;
bool isOKButtonPressed = false;

private void onPress(...) 
{
    lock(waitlock) 
    {
      isOKButtonPressed = true;
      if (isBackgroundWorkerCompleted) DoNextStep();
      else cursor.Wait(); //psuedo-code - use whatever the actual call is...
    }
}

Background thread does the same trick (just remember to BeginInvoke back to the main UI thread)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top