Permalink
Fetching contributors…
Cannot retrieve contributors at this time
55 lines (43 sloc) 4.81 KB
title ms.custom ms.date ms.prod ms.reviewer ms.suite ms.technology ms.tgt_pltfrm ms.topic helpviewer_keywords ms.assetid caps.latest.revision author ms.author manager
Pausing and Resuming Threads
03/30/2017
.net
dotnet-standard
article
resuming threads
threading [.NET Framework], pausing
pausing threads
9fce4859-a19d-4506-b082-7dd0792688ca
14
rpetrusha
ronpet
wpickett

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 xref:System.Threading.ThreadInterruptedException to break them out of their wait states.

The Thread.Sleep Method

Calling the xref:System.Threading.Thread.Sleep%2A?displayProperty=nameWithType method causes the current thread to immediately block for the number of milliseconds or the time interval you pass to the method, and yields the remainder of its time slice to another thread. Once that interval elapses, the sleeping thread resumes execution.

One thread cannot call xref:System.Threading.Thread.Sleep%2A?displayProperty=nameWithType on another thread. xref:System.Threading.Thread.Sleep%2A?displayProperty=nameWithType is a static method that always causes the current thread to sleep.

Calling xref:System.Threading.Thread.Sleep%2A?displayProperty=nameWithType with a value of xref:System.Threading.Timeout.Infinite?displayProperty=nameWithType causes a thread to sleep until it is interrupted by another thread that calls the xref:System.Threading.Thread.Interrupt%2A?displayProperty=nameWithType method on the sleeping thread, or until it is terminated by a call to its xref:System.Threading.Thread.Abort%2A?displayProperty=nameWithType method. The following example illustrates both methods of interrupting a sleeping thread.

[!code-csharpConceptual.Threading.Resuming#1] [!code-vbConceptual.Threading.Resuming#1]

Interrupting Threads

You can interrupt a waiting thread by calling the xref:System.Threading.Thread.Interrupt%2A?displayProperty=nameWithType method on the blocked thread to throw a xref:System.Threading.ThreadInterruptedException, which breaks the thread out of the blocking call. The thread should catch the xref:System.Threading.ThreadInterruptedException and do whatever is appropriate to continue working. If the thread ignores the exception, the runtime catches the exception and stops the thread.

[!NOTE] If the target thread is not blocked when xref:System.Threading.Thread.Interrupt%2A?displayProperty=nameWithType 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 xref:System.Threading.Thread.Interrupt%2A?displayProperty=nameWithType and xref:System.Threading.Thread.Abort%2A?displayProperty=nameWithType both wake the thread immediately. If a wait is an unmanaged wait (for example, a platform invoke call to the Win32 WaitForSingleObject function), neither xref:System.Threading.Thread.Interrupt%2A?displayProperty=nameWithType nor xref:System.Threading.Thread.Abort%2A?displayProperty=nameWithType can take control of the thread until it returns to or calls into managed code. In managed code, the behavior is as follows:

  • xref:System.Threading.Thread.Interrupt%2A?displayProperty=nameWithType wakes a thread out of any wait it might be in and causes a xref:System.Threading.ThreadInterruptedException to be thrown in the destination thread.

  • xref:System.Threading.Thread.Abort%2A?displayProperty=nameWithType wakes a thread out of any wait it might be in and causes a xref:System.Threading.ThreadAbortException to be thrown on the thread. For details, see Destroying Threads.

See Also

xref:System.Threading.Thread
xref:System.Threading.ThreadInterruptedException
xref:System.Threading.ThreadAbortException
Threading
Using Threads and Threading
Overview of Synchronization Primitives