Поделиться через


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.

The Thread.Sleep Method

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

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

Interrupting Threads

You can interrupt a waiting thread by calling System.Threading.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.

NoteNote

If the target thread is not blocked when System.Threading.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 System.Threading.Thread.Interrupt and System.Threading.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 System.Threading.Thread.Interrupt nor System.Threading.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:

  • System.Threading.Thread.Interrupt wakes a thread out of any wait it might be in and causes a ThreadInterruptedException to be thrown in the destination thread.

  • System.Threading.Thread.Abort is similar to System.Threading.Thread.Interrupt, except that it causes a ThreadAbortException to be thrown on the thread. For details, see Destroying Threads.

Suspend and Resume (Obsolete)

NoteImportant

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

You can also pause a thread by calling System.Threading.Thread.Suspend. When a thread calls System.Threading.Thread.Suspend on itself, the call blocks until the thread is resumed by another thread. When one thread calls System.Threading.Thread.Suspend on another thread, the call is a non-blocking call that causes the other thread to pause. Calling System.Threading.Thread.Resume breaks another thread out of the suspend state and causes the thread to resume execution, regardless of how many times System.Threading.Thread.Suspend was called. For example, if you call System.Threading.Thread.Suspend five consecutive times and then call System.Threading.Thread.Resume, the thread resumes execution immediately following the call to System.Threading.Thread.Resume.

Unlike System.Threading.Thread.Sleep, System.Threading.Thread.Suspend does not cause a thread to immediately stop execution. The common language runtime must wait until the thread has reached a safe point before it can suspend the thread. A thread cannot be suspended if it has not been started or if it has stopped. For details on safe points, see Thread.Suspend, Garbage Collection, and Safe Points.

NoteImportant

The System.Threading.Thread.Suspend and System.Threading.Thread.Resume methods are not generally useful for applications and should not be confused with synchronization mechanisms. Because System.Threading.Thread.Suspend and System.Threading.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 Priority property rather than System.Threading.Thread.Suspend.

See Also

Reference

Thread
ThreadInterruptedException
ThreadAbortException

Concepts

Overview of Synchronization Primitives
Thread.Suspend, Garbage Collection, and Safe Points

Other Resources

Managed Threading
Using Threads and Threading