Updated: August 2009
Suspends the current thread for a specified time.
Public Shared Sub Sleep ( _ millisecondsTimeout As Integer _ )
Dim millisecondsTimeout As Integer Thread.Sleep(millisecondsTimeout)
public static void Sleep( int millisecondsTimeout )
public: static void Sleep( int millisecondsTimeout )
public static function Sleep( millisecondsTimeout : int )
The time-out value is negative and is not equal to Infinite.
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.
This method does not perform standard COM and SendMessage pumping.
If you need to sleep on a thread that has STAThreadAttribute, but you want to perform standard COM and SendMessage pumping, consider using one of the overloads of the Join method that specifies a timeout interval.
The following example uses the Sleep method to block the application's main thread.
Imports System.Threading Class Example Shared Sub Main() For i As Integer = 0 To 4 Console.WriteLine("Sleep for 2 seconds.") Thread.Sleep(2000) Next Console.WriteLine("Main thread exits.") End Sub End Class ' This example produces the following output: ' 'Sleep for 2 seconds. 'Sleep for 2 seconds. 'Sleep for 2 seconds. 'Sleep for 2 seconds. 'Sleep for 2 seconds. 'Main thread exits.
using System; using System.Threading; class Example { static void Main() { for (int i = 0; i < 5; i++) { Console.WriteLine("Sleep for 2 seconds."); Thread.Sleep(2000); } Console.WriteLine("Main thread exits."); } } /* This example produces the following output: Sleep for 2 seconds. Sleep for 2 seconds. Sleep for 2 seconds. Sleep for 2 seconds. Sleep for 2 seconds. Main thread exits. */
using namespace System; using namespace System::Threading; int main() { for (int i = 0; i < 5; i++) { Console::WriteLine("Sleep for 2 seconds."); Thread::Sleep(2000); } Console::WriteLine("Main thread exits."); } /* This example produces the following output: Sleep for 2 seconds. Sleep for 2 seconds. Sleep for 2 seconds. Sleep for 2 seconds. Sleep for 2 seconds. Main thread exits. */
Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC, Xbox 360, Zune
Date
History
Reason
August 2009
Simplified example.
Customer feedback.