How do I wait for a BackgroundWorker to finish?

  • Thread starter Thread starter Tom
  • Start date Start date
T

Tom

I have a BackgroundWorker doing some work. I get to a place in the
application that I am dependent on the BackgroundWorker being done. The
WorkerCompleted routine can mark that it it compled simply enough but if it
is not compled what are my options for waiting and being notified that the
background work is complete?
 
Tom said:
I have a BackgroundWorker doing some work. I get to a place in the
application that I am dependent on the BackgroundWorker being done. The
WorkerCompleted routine can mark that it it compled simply enough but if
it
is not compled what are my options for waiting and being notified that the
background work is complete?

Windows Commutations Foundation which allows a programmer to use cross
application communications can be used. WCF Named Pipe or MS Message Queue
can be used to allow communications between programs, a polling mechanism
could be implemented.


__________ Information from ESET NOD32 Antivirus, version of virus signature database 4219 (20090705) __________

The message was checked by ESET NOD32 Antivirus.

http://www.eset.com
 
If i read you correctly, the easiest way is to have a boolean value set to
true and when the background workercompleted is fired, change this value to
false. In the main routine just do until boolean = false and loop. At this
point you can use the background workers built in features to send updates to
let you know how long it has to go.
 
I have a BackgroundWorker doing some work. I get to a place in the
application that I am dependent on the BackgroundWorker being done. The
WorkerCompleted routine can mark that it it compled simply enough but if it
is not compled what are my options for waiting and being notified that the
background work is complete?

Take a look at monitor.wait and monitor.pulse/monitor.pulseall

Your main thread can call monitor wait, which will block it until your
background thread calls monitor.pulse

You need to be careful here, if your main thread is the UI thread,
then blocking it will stall your UI

To keep your UI responsive, don't block, but disable parts of the UI
that shouldn't be accessed until the worker thread is complete, then
once the work is complete have the worker thread notify the main ui
thread (by calling control.invoke) and then enable the parts of the ui
that rely on the work being completed.

Matt
 
Hello John,

There's no need for any do/until construnctions here! by far the easiest
way is to use a WaitHandle:

before calling the worker thread create a AutoResetEvent:

AutoResetEvent waitHandle = new AutoResetEvent(false);

now, on the last line of the WorkerCompleted routine put:

waitHandle.Set();

and on your exitpoint (the window's Closing event?) call:

waitHandle.WaitOne();

That should let the code wait for the worker to complete.

If it is indeed a windows forms app, you could also just trap the Closing
event of the form, and check the status of the Worker and either cancel it's
operation, or set the e.Cancel to true in the event to prevent the form from
closing.

Jesse
 
Back
Top