Thread.Sleep Method (Int32)
Assembly: mscorlib (in mscorlib.dll)
public static void Sleep ( int millisecondsTimeout )
public static function Sleep ( millisecondsTimeout : int )
Parameters
- millisecondsTimeout
The number of milliseconds for which the thread is blocked. Specify zero (0) to indicate that this thread should be suspended to allow other waiting threads to execute. Specify Infinite to block the thread indefinitely.
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.
Note |
|---|
| 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.
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() ); } }
import System.*;
import System.Threading.*;
import System.Threading.Thread;
class ApartmentTest
{
public static void main(String[] args)
{
Thread newThread = new Thread(new ThreadStart(ThreadMethod));
newThread.set_ApartmentState(ApartmentState.MTA);
// The following line is ignored since
// ApartmentState can only be set once.
newThread.set_ApartmentState(ApartmentState.STA);
Console.WriteLine("ThreadState: {0}, ApartmentState: {1}",
newThread.get_ThreadState(),
newThread.get_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.set_ApartmentState(ApartmentState.STA);
}
catch (ThreadStateException stateException) {
Console.WriteLine("\n{0} caught:\n" +
"Thread is not in the Unstarted or Running state.",
stateException.GetType().get_Name());
Console.WriteLine("ThreadState: {0}, ApartmentState: {1}",
newThread.get_ThreadState(),newThread.get_ApartmentState());
}
} //main
static void ThreadMethod()
{
Thread.Sleep(1000);
} //ThreadMethod
} //ApartmentTest
Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.
Note