47

I have following problem:

I want to check (C#) if a thread has finished execution, i.e. if the thread method has returned. What I do now is call Thread.Join(1), but this gives a 1 ms delay. Is there any way to simply check if a thread has finished. Inspecting Thread.ThreadState just seems too cumbersome.

participant
  • 2,923
  • 2
  • 23
  • 40
Bogi
  • 2,274
  • 5
  • 26
  • 34

8 Answers8

78

Use the Thread.IsAlive flag. This is to give the thread status.

Brian Webster
  • 30,033
  • 48
  • 152
  • 225
Mohanavel
  • 1,763
  • 3
  • 22
  • 44
15

For a thread you have the myThread.IsAlive property. It is false if the thread method returned or the thread was aborted.

Daniel Rose
  • 17,233
  • 9
  • 65
  • 88
15

If you don't want to block the current thread by waiting/checking for the other running thread completion, you can implement callback method like this.

Action onCompleted = () => 
{  
    //On complete action
};

var thread = new Thread(
  () =>
  {
    try
    {
      // Do your work
    }
    finally
    {
      onCompleted();
    }
  });
thread.Start();

If you are dealing with controls that doesn't support cross-thread operation, then you have to invoke the callback method

this.Invoke(onCompleted);
Eric Brown - Cal
  • 14,135
  • 12
  • 58
  • 97
shanmuga raja
  • 685
  • 6
  • 19
11

You could fire an event from your thread when it finishes and subscribe to that.

Alternatively you can call Thread.Join() without any arguments:

Blocks the calling thread until a thread terminates, while continuing to perform standard COM and SendMessage pumping.

Thread.Join(1) will:

Blocks the calling thread until a thread terminates or the specified time elapses, while continuing to perform standard COM and SendMessage pumping.

In this case the specified time is 1 millisecond.

ChrisF
  • 134,786
  • 31
  • 255
  • 325
  • 5
    Before calling Thread.Join(), you should always check that the current thread is different than the one you are joining. Otherwise you'll never return. – Daniel Rose May 05 '10 at 13:25
  • @DanielRose Can you give a quick snippet on how to check if they are different? Thanks. – Ryan R Jan 21 '14 at 18:31
  • 1
    @RyanR Assuming `t1` is the thread you want to join: `if (Thread.CurrentThread != t1) t1.Join();` – Daniel Rose Jan 22 '14 at 09:24
10

Use Thread.Join(TimeSpan.Zero) It will not block the caller and returns a value indicating whether the thread has completed its work. By the way, that is the standard way of testing all WaitHandle classes as well.

Brian Webster
  • 30,033
  • 48
  • 152
  • 225
Brian Gideon
  • 47,849
  • 13
  • 107
  • 150
2

I use IsAlive extensively, unless I want to block the current execution (of the calling thread), in which case I just call Join() without a parameter. Now, be aware that IsAlive may return false if the target thread has not actually started execution yet for any reason.

Carlos Merighe.

1

Take a look at BackgroundWorker Class, with the OnRunWorkerCompleted you can do it.

Korteby Farouk
  • 629
  • 7
  • 14
  • 7
    Link only answers are bad answers because if the link ever changes your answer becomes useless. Please incorporate the relevant information in the answer and explain why what you are suggesting answers the question. – ChrisF Mar 30 '13 at 23:56
1

It depends on how you want to use it. Using a Join is one way. Another way of doing it is let the thread notify the caller of the thread by using an event. For instance when you have your graphical user interface (GUI) thread that calls a process which runs for a while and needs to update the GUI when it finishes, you can use the event to do this. This website gives you an idea about how to work with events:

http://msdn.microsoft.com/en-us/library/aa645739%28VS.71%29.aspx

Remember that it will result in cross-threading operations and in case you want to update the GUI from another thread, you will have to use the Invoke method of the control which you want to update.

intodev
  • 59
  • 2