Mark Carpenter
Mark Carpenter

Reputation: 17775

Does Thread.Sleep affect ThreadState?

If I create and start a thread, how does calling Thread.Sleep(x) within that thread affect the ThreadState (if at all)?

Thanks!

Upvotes: 2

Views: 2345

Answers (5)

ChrisF
ChrisF

Reputation: 137178

I don't want this to come over as a sarcastic answer, as that won't help anyone - so please take this in the spirit that it was intended.

Have you tried creating a simple winform app with buttons to start, stop and sleep a thread and a status area to show the value of thread.ThreadState?

This will answer your question.

Upvotes: 3

Reed Copsey
Reed Copsey

Reputation: 564751

The thread should be put into ThreadState.WaitSleepJoin.

For details, see ThreadState's Documentation, specifically:

WaitSleepJoin: "The thread is blocked. This could be the result of calling Thread..::.Sleep or Thread..::.Join, of requesting a lock — for example, by calling Monitor..::.Enter or Monitor..::.Wait — or of waiting on a thread synchronization object such as ManualResetEvent. "

Upvotes: 1

jerryjvl
jerryjvl

Reputation: 20151

It transitions to WaitSleepJoin.

Upvotes: 1

Mitch Wheat
Mitch Wheat

Reputation: 300728

ThreadState defines a set of all possible execution states for threads. Once a thread is created, it is in at least one of the states until it terminates. Threads created within the common language runtime are initially in the Unstarted state, while external threads that come into the runtime are already in the Running state. An Unstarted thread is transitioned into the Running state by calling Start. Not all combinations of ThreadState values are valid; for example, a thread cannot be in both the Aborted and Unstarted states.

Important: Thread state is only of interest in a few debugging scenarios. Your code should never use thread state to synchronize the activities of threads.

ThreadState: WaitSleepJoin: The thread is blocked as a result of a call to Wait, Sleep, or Join.

From here.

Upvotes: 4

Luca Martinetti
Luca Martinetti

Reputation: 3412

From MSDN

WaitSleepJoin The thread is blocked. This could be the result of calling Thread.Sleep or Thread.Join, of requesting a lock — for example, by calling Monitor.Enter or Monitor.Wait — or of waiting on a thread synchronization object such as ManualResetEvent.

Short answer is: Yes!

Upvotes: 4

Related Questions