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 code example demonstrates a sleeping thread.
Imports Microsoft.VisualBasic Imports System Imports System.Threading Public Class ApartmentTest <MTAThread> _ Shared Sub Main() Dim newThread As Thread = New Thread(AddressOf ThreadMethod) newThread.SetApartmentState(ApartmentState.MTA) ' The following line is ignored since ' ApartmentState can only be set once. newThread.SetApartmentState(ApartmentState.STA) Console.WriteLine("ThreadState: {0}, ApartmentState: {1}", _ newThread.ThreadState, newThread.GetApartmentState()) newThread.Start() ' Wait for newThread to start and go to sleep. Thread.Sleep(300) Try ' This causes an exception since newThread is sleeping. newThread.SetApartmentState(ApartmentState.STA) Catch stateException As ThreadStateException Console.WriteLine(vbCrLf & "{0} caught:" & vbCrLf & _ "Thread is not In the Unstarted or Running state.", _ stateException.GetType().Name) Console.WriteLine("ThreadState: {0}, ApartmentState: " & _ "{1}", newThread.ThreadState, newThread.GetApartmentState()) End Try End Sub Shared Sub ThreadMethod() Thread.Sleep(1000) End Sub End Class
using System; using System.Threading; class ApartmentTest { static void Main() { Thread newThread = new Thread(new ThreadStart(ThreadMethod)); newThread.SetApartmentState(ApartmentState.MTA); // The following line is ignored since // ApartmentState can only be set once. newThread.SetApartmentState(ApartmentState.STA); Console.WriteLine("ThreadState: {0}, ApartmentState: {1}", newThread.ThreadState, newThread.ApartmentState); newThread.Start(); // Wait for newThread to start and go to sleep. Thread.Sleep(300); try { // This causes an exception since newThread is sleeping. newThread.SetApartmentState(ApartmentState.STA); } catch(ThreadStateException stateException) { Console.WriteLine("\n{0} caught:\n" + "Thread is not in the Unstarted or Running state.", stateException.GetType().Name); Console.WriteLine("ThreadState: {0}, ApartmentState: {1}", newThread.ThreadState, newThread.GetApartmentState()); } } static void ThreadMethod() { Thread.Sleep(1000); } }
using namespace System; using namespace System::Threading; ref class ApartmentTest { public: static void ThreadMethod() { Thread::Sleep( 1000 ); } }; int main() { Thread^ newThread = gcnew Thread( gcnew ThreadStart( &ApartmentTest::ThreadMethod ) ); newThread->SetApartmentState(ApartmentState::MTA); // The following line is ignored since // ApartmentState can only be set once. newThread->SetApartmentState(ApartmentState::STA); Console::WriteLine( "ThreadState: {0}, ApartmentState: {1}", newThread->ThreadState.ToString(), newThread->GetApartmentState().ToString() ); newThread->Start(); // Wait for newThread to start and go to sleep. Thread::Sleep( 300 ); try { // This causes an exception since newThread is sleeping. newThread->SetApartmentState(ApartmentState::STA); } catch ( ThreadStateException^ stateException ) { Console::WriteLine( "\n{0} caught:\n" "Thread is not in the Unstarted or Running state.", stateException->GetType()->Name ); Console::WriteLine( "ThreadState: {0}, ApartmentState: {1}", newThread->ThreadState.ToString(), newThread->GetApartmentState().ToString() ); } }
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