|
-
Sep 28th, 2011, 10:41 AM
#1
Thread Starter
New Member
[SOLVED]Thread message when all threads finished
Okey I wrote simple code like this->
csharp Code:
Thread[] ts=new Thread[10];
for(int i=0;i<10;i++)
{
ts[i]=new Thread(function);
ts[i].IsBackground = true;
ts[i].Start();
}
So my threads are doing some work in background... but I need some kind of MessageBox when all threads stops working... I don`t mean .Join() because it blocks the main thread which is form application...
I tried making another function to check background if ThreadState is stopped... but it doesn`t work...
Is there any way to show that MessageBox when all are stoppped(not on button stopped-aborted) ?
and I can`t change to threadpool or tasks....
Last edited by globus25; Jan 28th, 2012 at 04:11 PM.
Reason: SOLVED
-
Sep 28th, 2011, 10:23 PM
#2
Re: Thread message when all threads finished
Set a variable to the number of tasks. At the end of the thread entry method, atomically decrement that counter and, if it is zero, use an appropriate method to provide notification, e.g. marshal a method call back to the UI thread and update the UI.
-
Sep 29th, 2011, 02:00 AM
#3
Thread Starter
New Member
Re: Thread message when all threads finished
I already tried that but my threads some kind of strange(they have inside 2 loops that are working and one runs the other one) so any of that counters can`t work
-
Sep 29th, 2011, 02:16 AM
#4
Re: Thread message when all threads finished
That makes no sense. It doesn't matter if you have nested loops in your threaded routine, decrement your counter at the end of the method, should the counter be equal to 0 after decrementing you will know that all threads has finished.
-
Sep 29th, 2011, 02:44 AM
#5
Thread Starter
New Member
Re: Thread message when all threads finished
I already tried counters and they don`t work.. I tried mutex, lock still no working...
I wouldn`t be asking here if it was that simple
-
Sep 29th, 2011, 02:53 AM
#6
Re: Thread message when all threads finished
I'm guessing that your thread function contain multiple points of exit.
In that case, you should either redesign the thread function, or encapsulate it in a try-finally, having the finally block decrement the thread count.
If that's not the problem either, and you're desperate to get help, post the actual code..
It would be so much easier if we wouldn't have to guess where your problem is.
Delete it. They just clutter threads anyway.
-
Sep 29th, 2011, 03:01 AM
#7
Thread Starter
New Member
Re: Thread message when all threads finished
I will post part of code that Im trying to make work later today so U don`t need to guess...
-
Oct 6th, 2011, 02:15 PM
#8
PowerPoster
Re: Thread message when all threads finished
can you define what you mean by "when all threads stop working" - do you mean when they complete (successfully/unsuccessfully) or another way?
you could use manual/auto reset events, then use WaitHandle.WaitAll to indicate/show when all threads have signaled.
-
Jan 28th, 2012, 04:09 PM
#9
Thread Starter
New Member
Re: Thread message when all threads finished
I already figured it out 
Like Techno said manual reset events works good
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|