Results 1 to 6 of 6

Thread: Waiting for a thread to finish? [C# 2002]

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2003
    Posts
    308

    Question Waiting for a thread to finish? [C# 2002]

    Okay - this is what my code is trying to accomplish (in relative pseudo-code):

    Code:
    SetStatusBar("Start Operation 1");
    SetLabel("Start Operation 1");
    Thread th1 = new Thread (new ThreadStart(Operation1));
    th1.Start();
    // WAIT FOR th1 to finish without loosing focus on the UI
    SetStatusBar("Operation 1 Finished");
    SetLabel("Operation 1 Finished");
    
    SetStatusBar("Start Operation 2");
    SetLabel("Start Operation 2");
    Thread th2 = new Thread (new ThreadStart(Operation2));
    th2.Start();
    // WAIT FOR th2 to finish without loosing focus on the UI
    SetStatusBar("Operation 2 Finished");
    SetLabel("Operation 2 Finished");
    
    SetStatusBar("Start Operation 3");
    SetLabel("Start Operation 3");
    Thread th3 = new Thread (new ThreadStart(Operation3));
    th3.Start();
    // WAIT FOR th3 to finish without loosing focus on the UI
    SetStatusBar("Operation 3 Finished");
    SetLabel("Operation 3 Finished");
    
    btnCanCloseWindow.Enabled = true;	// this can only be true if all the tasks have run
    That is pretty much a dumb'ed down idea of how my application works... (where the thread functions OperationX will launch corresponding processes). As it is this works fine except that I am unable to find a way to WAIT for a thread to complete without holding the form itself hostage.

    I tried using "thX.Join()" but this will block the UI thread until it returns (which is not ok)... while each operation is running I need the user to be able to move the window (UI) if he wants to, minimize it, or simply stare at it to see what is being done by looking at the status bar or label... But it can't look out of focus or as if it has frozen...

    Any help would be much appreciated.
    Thanks,

  2. #2
    Registered User nmadd's Avatar
    Join Date
    Jun 2007
    Location
    U.S.A.
    Posts
    1,676

    Re: Waiting for a thread to finish? [C# 2002]

    Hi,
    I'll take a stab at this one; and that's just what it is, so take it with a grain of salt as this could be wrong.

    The documentation says that Thread.Join() blocks the calling thread until a thread terminates. So, what about calling all of your threads, including the UI, from a different "main" thread. Will something like that even work? Just a shot in the dark.

    ....or just wait until jmcilhinney logs on later tonight and gives you the simplest of answers for what I'm babbling on about.

    Good luck.
    Last edited by nmadd; Jul 20th, 2007 at 04:07 PM.

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2003
    Posts
    308

    Re: Waiting for a thread to finish? [C# 2002]

    That is hilarious - but it might actually work (I'll test it out in a bit)...
    But to be sure - you mean create a "master thread" and have it spawn the UI thread as well as the worker threads? That way the MASTER can block until all workers come back and somehow notify the UI that it can now proceed to do what needs to be done - all the while not blocking the UI itself...

    ...wow...
    indepth for something I imagined to be a lot simpler...
    ... lol ...

  4. #4
    Registered User nmadd's Avatar
    Join Date
    Jun 2007
    Location
    U.S.A.
    Posts
    1,676

    Re: Waiting for a thread to finish? [C# 2002]

    Quote Originally Posted by Shaitan00
    That is hilarious - but it might actually work (I'll test it out in a bit)...
    But to be sure - you mean create a "master thread" and have it spawn the UI thread as well as the worker threads? That way the MASTER can block until all workers come back and somehow notify the UI that it can now proceed to do what needs to be done - all the while not blocking the UI itself...

    ...wow...
    indepth for something I imagined to be a lot simpler...
    ... lol ...
    I really don't know if there is an easier way (I imagine there is), but yep, that's the hilarious idea.

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

    Re: Waiting for a thread to finish? [C# 2002]

    So, the stuff that all these threads will do....why cant you just do it in one thread?
    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)

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

    Re: Waiting for a thread to finish? [C# 2002]

    As the last action in your thread invoke a delegate on the UI thread. This is basically equivalent to what a .NET 2.0 BackgroundWorker does when it raises its RunWorkerCompleted event.
    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

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