Thread.Sleep Method (TimeSpan)
[ This article is for Windows Phone 8 developers. If you’re developing for Windows 10, see the latest documentation. ]
Blocks the current thread for a specified time.
Namespace: System.Threading
Assembly: mscorlib (in mscorlib.dll)
Syntax
Public Shared Sub Sleep ( _
timeout As TimeSpan _
)
public static void Sleep(
TimeSpan timeout
)
Parameters
- timeout
Type: System..::.TimeSpan
A TimeSpan set to the amount of time for which the thread is blocked. Specify a duration of 1 millisecond to yield to other threads that might be waiting to execute. (We recommend a wait of 1 millisecond instead of a wait of 0.) Specify a duration of -1 milliseconds to block the thread indefinitely.
Exceptions
Exception | Condition |
---|---|
ArgumentOutOfRangeException | The value of timeout is negative and is not equal to -1 milliseconds, or it is greater than Int32..::.MaxValue milliseconds. |
Remarks
The thread will not be scheduled for execution by the operating system for the amount of time specified. This method changes the state of the thread to include WaitSleepJoin.
Important Note: |
---|
Do not call Sleep from the main application thread in your Windows Phone application. Doing so makes the user interface (UI) unresponsive, blocks DispatcherTimer and BackgroundWorker events, and blocks cross-thread calls to the UI thread that were made by using Dispatcher. |
This overload of Sleep uses the total number of whole milliseconds in timeout. Fractional milliseconds are discarded.
This method does not perform standard COM and SendMessage pumping.
Note
If you need to sleep, but you want to perform standard COM and SendMessage pumping, consider using one of the overloads of the Join method that specifies a time-out interval. For example, replace Sleep(1000) with Thread.CurrentThread.Join(1000).
Version Information
Windows Phone OS
Supported in: 8.1, 8.0, 7.1, 7.0
Platforms
Windows Phone