Pausing and Resuming Threads

Pausing and Resuming Threads


The most common ways to synchronize the activities of threads are to block and release threads, or to lock objects or regions of code. For more information on these locking and blocking mechanisms, see Overview of Synchronization Primitives.

You can also have threads put themselves to sleep. When threads are blocked or sleeping, you can use a ThreadInterruptedException to break them out of their wait states.

Calling the Thread.Sleep method causes the current thread to immediately block for the number of milliseconds you pass to Thread.Sleep, yielding the remainder of its time slice to another thread. One thread cannot call Thread.Sleep on another thread.

Calling Thread.Sleep with Timeout.Infinite causes a thread to sleep until it is interrupted by another thread that calls Thread.Interrupt, or until it is terminated by Thread.Abort.

You can interrupt a waiting thread by calling Thread.Interrupt on the blocked thread to throw a ThreadInterruptedException, which breaks the thread out of the blocking call. The thread should catch the ThreadInterruptedException and do whatever is appropriate to continue working. If the thread ignores the exception, the runtime catches the exception and stops the thread.


If the target thread is not blocked when Thread.Interrupt is called, the thread is not interrupted until it blocks. If the thread never blocks, it could complete without ever being interrupted.

If a wait is a managed wait, then Thread.Interrupt and Thread.Abort both wake the thread immediately. If a wait is an unmanaged wait (for example, a platform invoke call to the Win32 WaitForSingleObject function), neither Thread.Interrupt nor Thread.Abort can take control of the thread until it returns to or calls into managed code. In managed code, the behavior is as follows:

The Thread class includes two methods, Thread.Suspend and Thread.Resume, for pausing and resuming a thread. However, the use of these methods is not recommended.


Starting with the .NET Framework version 2.0, the Thread.Suspend and Thread.Resume methods are marked obsolete and will be removed in a future release.

The Thread.Suspend and Thread.Resume methods are not generally useful for applications and should not be confused with synchronization mechanisms. Because Thread.Suspend and Thread.Resume do not rely on the cooperation of the thread being controlled, they are highly intrusive and can result in serious application problems like deadlocks (for example, if you suspend a thread that holds a resource that another thread will need).

Some applications do need to control the priority of threads for better performance. To do this, you should use the Thread.Priority property rather than Thread.Suspend.

© 2016 Microsoft