Best practice for waiting for worker thread to complete

  • Thread starter Thread starter MarkR
  • Start date Start date
M

MarkR

Hello:

I would like to know the best way to spend idle cycles while waiting for a
thread to complete. I've seen numerous conversations on this group about how
unnatural Application.DoEvents() is, but can't find a good recommendation on
what I *should* do.

<Background>
I have inherited a C# state-machine application: i.e. Step 2 begins when
Step 1 is completed; Step 3 when Step 2 completes, etc. The code is
implemented within one controller function, rather than being event-based,
that looks something like:

Controller()
{
BeginInvoke(ProducerThread);

DisplayPageSynchronous(Page1);
int result = DisplayPageSynchronous(Page2);
DisplayPageSynchronous((result == 1 ? Page3 : Page4));
...
}

Page2 is a thumbnail view of the data the ProducerThread produces. In the
legacy codebase, Page2 blocked with a Monitor.Enter call; I would like to
add either progressive display of the thumbnails or a simple progress
indicator.

Since, unfortunately, Page2 is displayed synchronously, I need its message
pump to keep running while it waits for the producer to complete, which
really sounds like a job for a while() loop with a DoEvents/Sleep pair
inside it. But the wisdom of this group is to avoid such a call.

Thus, my question: What is the best practice for waiting for a thread to
finish? Am I missing something I could do with, e.g. the AutoResetEvent or
Monitor.Pulse?


Thank you for any and all help,
/m
 
You can either set up an IAsync, which I think is painful and a lot of
work, or you can add a backgroundworker control (2.0), and the
workercompleted even will fire when its done... (you can do whatever you
want in that time, including being idle)...

one page with a few references to the background worker control is here:
http://weblogs.asp.net/rosherove/archive/2004/06/16/156948.aspx

this one is also popular:
http://www.mikedub.net/mikeDubSampl...eallySimpleMultithreadingInWindowsForms20.htm

or this is a fairly good video on IAsync by Mike Taulty:
http://www.microsoft.com/uk/asx/msdn/nuggets/asynchronouswebservicecalls.asx

Goodluck...
 
Thus, my question: What is the best practice for waiting for a thread to
finish? Am I missing something I could do with, e.g. the AutoResetEvent or
Monitor.Pulse?

Your UI thread should just keep running the message pump, not in your
code. Disable all the controls which you want to be inaccessible while
the worker thread is running, and then make the worker thread call back
to the form when it's finished.
 
Thank you both for your replies. It appears you're both advocating switing
over to a more event-driven flow, which makes total sense, rather than a
kludgey wait loop.

I appreciate your help,
/m
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top