473,890 Members | 1,680 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

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<ThreadElen coThreadTabelle = new List<Thread>();

....
....

populate list and starting threads...

....
....

while (ElencoThreadTa belle.Count 0)
{
if (!ElencoThreadT abelle[0].IsAlive)
{
ElencoThreadTab elle.RemoveAt(0 );
}

Application.DoE vents();
Thread.Sleep(25 0);
}

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.
Dec 21 '06 #1
3 7167


"Ing. Davide Piras" <da****@naxosof tware.comwrote in message
news:O5******** ******@TK2MSFTN GP03.phx.gbl...
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<ThreadElen coThreadTabelle = new List<Thread>();

...
...

populate list and starting threads...

...
...

while (ElencoThreadTa belle.Count 0)
{
if (!ElencoThreadT abelle[0].IsAlive)
{
ElencoThreadTab elle.RemoveAt(0 );
}

Application.DoE vents();
Thread.Sleep(25 0);
}

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 ElencoThreadTab elle)
{
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

Dec 21 '06 #2
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.BeginI nvoke
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.BeginIn voke
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

Dec 21 '06 #3
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 ManualResetEven t, 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.

--
Chris Mullins, MCSD.NET, MCPD:Enterprise
http://www.coversant.net/blogs/cmullins

"Ing. Davide Piras" <da****@naxosof tware.comwrote in message
news:O5******** ******@TK2MSFTN GP03.phx.gbl...
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<ThreadElen coThreadTabelle = new List<Thread>();

...
...

populate list and starting threads...

...
...

while (ElencoThreadTa belle.Count 0)
{
if (!ElencoThreadT abelle[0].IsAlive)
{
ElencoThreadTab elle.RemoveAt(0 );
}

Application.DoE vents();
Thread.Sleep(25 0);
}

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.

Dec 22 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

44
2388
by: Charles Law | last post by:
Hi guys. I'm back on the threading gig again. It's the age-old question about waiting for something to happen without wasting time doing it. Take two threads: the main thread and a worker thread. The worker thread is reading the serial port, waiting for something to happen (a service request). When it does it raises an event. Of course, the event is executed on the worker thread. The idea is that when the event is raised, the handler...
5
1373
by: opistobranchia | last post by:
I know this gets asked all the time but..... I am a fresh grad. I have taken a year of java and 1/2 year of C++ and OO Design and Analysis. I have strong Java skills and I have done projects in c++. But I am pretty sure that I am getting this job as a java/C++ software engineer. I want to put out a good impression since it is with a real good company and a great opportunity for me with real smart people. What book do you rec. that I...
5
3858
by: bughunter | last post by:
Hi, Consider this code: ---- Monitor.Pulse(oLock); Monitor.Exit(oLock); ---- If a thread was waiting on oLock then will the current thread
13
4405
by: ALI-R | last post by:
Hi All, When we say events are processed asynchronously in for instance Sharepoint ,what dose it mean? Thanks for your help. Ali
4
3204
by: Charles Law | last post by:
I've been using monitors a bit lately (some of you may have heard ;-) ) and then up pop Manual and AutoResetEvents , and they look for all the world like the same thing. Are they interchangeable, or when should I use one over the other? TIA Charles
5
3553
by: Soren S. Jorgensen | last post by:
Hi, In my app I've got a worker thread (background) doing some calculations based upon user input. A new worker thread might be invoked before the previous worker thread has ended, and I wan't only one worker thread running at any time (if a new worker thread start has been requested, any running worker thread results will be invalid). I'm using the below method to invoke a new worker thread, but when stress testing this I'm sometimes...
3
1711
by: Michael D. Ober | last post by:
If I use standard Threading.Thread threads, I can issue a Join on the thread variable and wait for it to complete. How can I do this on the Background Worker Threads in a threadpool without looping. Basically, I will be queuing an unknown number of worker threads in the pool and I need to wait for them all to complete. Thanks, Mike Ober.
8
1905
by: ecir.hana | last post by:
Dear list, maybe I'm overlooking something obvious or this is not possible at all or I don't know. Please, consider the following code: ## insert here anything you like def changer():
4
4536
by: Dave | last post by:
I have a global.asax file with Application_Start defined and create some static data there and in another module used in the asp.net application and I realize that static data is shared amongst child apps of an IIS application and can be used by multiple users during the application life cycle and for multiple page loads for the same or different page under a root application. What I don't understand and need to know is whether that...
0
9816
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10802
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10452
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9618
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
8009
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7161
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4667
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4260
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3268
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.