Andrew Laughlin
Andrew Laughlin

Reputation: 2032

When does Thread.CurrentThread.Join() make sense?

What is the effect of calling Thread.CurrentThread.Join(), and if/when would it make sense to call it?

Upvotes: 13

Views: 8694

Answers (6)

Andrew Rueckert
Andrew Rueckert

Reputation: 5225

CurrentThread.Join() can be used to put the current thread to sleep until another thread interrupts it.

For example, you may have a server where the main method sets up a pool of other threads to handle incoming requests, passes a reference to it's current thread to a shutdown-trap, and then goes to sleep until it's time for the server to shut down.

This is not a terribly common pattern but it would be wrong to say that there is no case where you'd want your current thread to sleep until interrupted.

Upvotes: 0

Herman Van Der Blom
Herman Van Der Blom

Reputation: 812

If you are making a unit test that tests if timers perform well in lets say a Windows Service and you use Thread.Sleep() statements with more as 60 seconds in it you can get ContextSwitch errors because the Thread.Sleep() is blocking the message pump.

If you are replacing those Thread.Sleep() statements in your unit test with Thread.CurrentThread.Join() then those ContextSwitch error will go away. So its a non blocking solution.

You could say Thread.CurrentThread.Join() is a better Thread.Sleep().

Upvotes: 1

rohit
rohit

Reputation: 902

It actually make sense in world of observable. Lets say you have a queue listener in main and you want to keep main thread running forever. Instead of doing while(true) and put your code in the loop, last line you can write this. This way current thread will also be parent thread for other threads spawned within the application. Think of it as entry point for app.

Upvotes: 3

Evgeniy Berezovsky
Evgeniy Berezovsky

Reputation: 19248

Was it really

CurrentThread.Join()

that you saw in real code - which I kind of doubt, unless it's some hack to prevent other threads to join on the current thread - or was it

CurrentThread.Join(someTimeout)

The latter is equivalent to

Thread.Sleep(someTimeout)

except that joining on the current thread allows message pumping to continue if you are in a GUI / COM situation.

Upvotes: 19

Reed Copsey
Reed Copsey

Reputation: 564691

What is the effect of calling Thread.CurrentThread.Join()

You will block the execution of the current thread, and effectively dead lock it. It will cause the current thread to block until the current thread finishes, which will never happen.

, and if/when would it make sense to call it?

It really doesn't make sense to do this. You should never call this method in this manner.


On a side note, since you're using .NET 4, I would recommend avoiding using Thread.Join in general. Using the new Task/Task<T> classes is far nicer in many ways, as you can easily attach continuations (or always call Task.Wait() if you truly need to block).

Upvotes: 16

Kau&#234; Gimenes
Kau&#234; Gimenes

Reputation: 1286

No, CurrentThread.Join() makes no sense
This could make your program stop running, making the thread A wait for thread A for example.

Upvotes: 2

Related Questions