Results 1 to 9 of 9

Thread: [SOLVED]Thread message when all threads finished

  1. #1

    Thread Starter
    New Member
    Join Date
    Sep 2011
    Posts
    10

    [SOLVED]Thread message when all threads finished

    Okey I wrote simple code like this->
    csharp Code:
    1. Thread[] ts=new Thread[10];
    2.  
    3. for(int i=0;i<10;i++)
    4. {
    5. ts[i]=new Thread(function);
    6. ts[i].IsBackground = true;
    7. ts[i].Start();
    8. }

    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

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    New Member
    Join Date
    Sep 2011
    Posts
    10

    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

  4. #4
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    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.
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  5. #5

    Thread Starter
    New Member
    Join Date
    Sep 2011
    Posts
    10

    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

  6. #6
    Frenzied Member TheBigB's Avatar
    Join Date
    Mar 2006
    Location
    *Stack Trace*
    Posts
    1,511

    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.

  7. #7

    Thread Starter
    New Member
    Join Date
    Sep 2011
    Posts
    10

    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...

  8. #8
    PowerPoster
    Join Date
    Aug 2003
    Location
    Edinburgh, UK
    Posts
    2,773

    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.

    MVP 2007-2010 any chance of a regain?
    Professional Software Developer and Infrastructure Engineer.

  9. #9

    Thread Starter
    New Member
    Join Date
    Sep 2011
    Posts
    10

    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
  •  



Click Here to Expand Forum to Full Width