Thread.Sleep メソッド (TimeSpan)
アセンブリ: mscorlib (mscorlib.dll 内)



スレッドは、指定した時間の間は、オペレーティング システムによる実行がスケジュールされません。このメソッドは、スレッドの状態を変更して、WaitSleepJoin の状態が含まれるようにします。
Sleep のこのオーバーロードは、timeout のミリ秒の整数部の合計を使用します。ミリ秒の小数部は、切り捨てられます。
このメソッドは、標準 COM/SendMessage ポンピングを実行しません。
![]() |
---|
STAThreadAttribute を持つスレッドでスリープする必要があり、標準 COM/SendMessage ポンピングを実行する必要がある場合は、タイムアウト間隔を指定する Join メソッドのいずれかのオーバーロードを使用することを検討してください。 |

Sleep メソッドで TimeSpan 値を使用する方法の例を次に示します。
Imports System Imports System.Threading Public Class Test Shared waitTime As New TimeSpan(0, 0, 1) <MTAThread> _ Shared Sub Main() Dim newThread As New Thread(AddressOf Work) newThread.Start() If newThread.Join( _ TimeSpan.op_Addition(waitTime, waitTime)) Then Console.WriteLine("New thread terminated.") Else Console.WriteLine("Join timed out.") End If End Sub Shared Sub Work() Thread.Sleep(waitTime) End Sub End Class
using System; using System.Threading; class Test { static TimeSpan waitTime = new TimeSpan(0, 0, 1); public static void Main() { Thread newThread = new Thread(new ThreadStart(Work)); newThread.Start(); if(newThread.Join(waitTime + waitTime)) { Console.WriteLine("New thread terminated."); } else { Console.WriteLine("Join timed out."); } } static void Work() { Thread.Sleep(waitTime); } }
using namespace System; using namespace System::Threading; static TimeSpan waitTime = TimeSpan(0,0,1); ref class Test { public: static void Work() { Thread::Sleep( waitTime ); } }; int main() { Thread^ newThread = gcnew Thread( gcnew ThreadStart( Test::Work ) ); newThread->Start(); if ( newThread->Join( waitTime + waitTime ) ) { Console::WriteLine( "New thread terminated." ); } else { Console::WriteLine( "Join timed out." ); } }
import System.*; import System.Threading.*; import System.Threading.Thread; class Test { private static TimeSpan waitTime = new TimeSpan(0, 0, 1); public static void main(String[] args) { Thread newThread = new Thread(new ThreadStart(Work)); newThread.Start(); if (newThread.Join((waitTime.Add(waitTime)))) { Console.WriteLine("New thread terminated."); } else { Console.WriteLine("Join timed out."); } } //main static void Work() { Thread.Sleep(waitTime); } //Work } //Test

Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


Thread.Sleep メソッド (Int32)
アセンブリ: mscorlib (mscorlib.dll 内)



スレッドは、指定した時間の間は、オペレーティング システムによる実行がスケジュールされません。このメソッドは、スレッドの状態を変更して、WaitSleepJoin の状態が含まれるようにします。
このメソッドは、標準 COM/SendMessage ポンピングを実行しません。
![]() |
---|
STAThreadAttribute を持つスレッドでスリープする必要があり、標準 COM/SendMessage ポンピングを実行する必要がある場合は、タイムアウト間隔を指定する Join メソッドのいずれかのオーバーロードを使用することを検討してください。 |

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() ); } }
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
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


Thread.Sleep メソッド
- Thread.Sleepのページへのリンク