What to do while waiting worker threads to finish their job...

I

Ing. Davide Piras

Hi there,
in my c# .NET 2.0 application I run few threads (from 6 to 12 it depends...)
and then my GUI Thread should wait all of them have finished their task...

so after create those threads i put them in a List<>, I start them one by
one then I do something like:

List<Thread> ElencoThreadTabelle = new List<Thread>();

....
....

populate list and starting threads...

....
....

while (ElencoThreadTabelle.Count > 0)
{
if (!ElencoThreadTabelle[0].IsAlive)
{
ElencoThreadTabelle.RemoveAt(0);
}

Application.DoEvents();
Thread.Sleep(250);
}

I don't know this is a good way, I think there should be something better, I
think can use thread.Join() but how to use it (if this is the best way) ?

in my context, any thread executes some works then calls thread.abort() at
the end of the threadProc so my main thread (the GUI) in the snipped above
doesn't need to kill threads, only to wait 'till they are alive then
continue when no one of them is running anymore...

Thanks, Regards, Davide.
 
D

David Browne

Ing. Davide Piras said:
Hi there,
in my c# .NET 2.0 application I run few threads (from 6 to 12 it
depends...) and then my GUI Thread should wait all of them have finished
their task...

so after create those threads i put them in a List<>, I start them one by
one then I do something like:

List<Thread> ElencoThreadTabelle = new List<Thread>();

...
...

populate list and starting threads...

...
...

while (ElencoThreadTabelle.Count > 0)
{
if (!ElencoThreadTabelle[0].IsAlive)
{
ElencoThreadTabelle.RemoveAt(0);
}

Application.DoEvents();
Thread.Sleep(250);
}

I don't know this is a good way, I think there should be something better,
I think can use thread.Join() but how to use it (if this is the best way)
?

Just loop through the threads and join each one.

foreach (Thread t in ElencoThreadTabelle)
{
t.Join();
}
in my context, any thread executes some works then calls thread.abort() at
the end of the threadProc so my main thread (the GUI) in the snipped above
doesn't need to kill threads, only to wait 'till they are alive then
continue when no one of them is running anymore...

No need for the threads to abort themselves. Just return from the thread
proc and the thread will die.


David
 
M

Marc Gravell

IMO neither thread should be "aborting" or "killing"... just let it
exit gracefully. Equally, I don't think the UI thread should be
actively looking at them. In this scenario, I would probably use a
callback, either by simply using the async Delegate.BeginInvoke
approach, or by manually using some other delegate / event setup at the
end of the thread lifetime. I tend to use the latter. At the UI, I
would respond to this event / callback by doing a Control.BeginInvoke
to notify the UI thread, and then probably just decrement a counter.
Once the counter is zero, all threads are done, so we can contine. But
note I didn't mention Sleep, DoEvents or Abort.

Join may have a role to play, but it doesn't seem necessary. Join is
used when you want a thread to block and wait for another thread to
finish, but we absolutely don't want to block the UI thread, so don't
call Join. Of course, you could use an additional controller thread to
manage this; the controller ("one thread to rule them all and in the
thread-pool bind them") could then simply Join all the other threads.
Not sure it helps much, though.

Marc
 
C

Chris Mullins

If you've only got 6 to 12 threads, you probably don't want to be creating
your own threads at all.

The System Threadpool is excellent for queuing up a few work items. Pass in
some state that contains a ManualResetEvent, and when your processing in
done, as the last step tickle the Manual Reset Event so that your main GUI
thread can tell where things are at.

This will let you not have to worry about managing the lifetime of any
threads, which can be a non-trivial excercise in some cases.
 

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

Top