Thread.Sleepとは? わかりやすく解説

Thread.Sleep メソッド (TimeSpan)

指定した時間の間現在のスレッドブロックします

名前空間: System.Threading
アセンブリ: mscorlib (mscorlib.dll 内)
構文構文

例外例外
例外種類条件

ArgumentOutOfRangeException

timeout の値が負で、ミリ秒単位Timeout.Infinite等しくないか、または MaxValue ミリ秒より大きいです。

解説解説
使用例使用例

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
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照

Thread.Sleep メソッド (Int32)

指定した時間だけ現在のスレッド中断します

名前空間: System.Threading
アセンブリ: mscorlib (mscorlib.dll 内)
構文構文

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 void Sleep
 (
    int millisecondsTimeout
)
public static function Sleep
 (
    millisecondsTimeout : int
)

パラメータ

millisecondsTimeout

スレッドブロックされるミリ秒数。待機中の他のスレッド実行できるように、このスレッド中断を示すには 0 を指定しますスレッド無制限にブロックするには Infinite指定します

例外例外
例外種類条件

ArgumentOutOfRangeException

タイムアウト値が負か、Infinite等しくありません。

解説解説

スレッドは、指定した時間の間は、オペレーティング システムによる実行スケジュールされません。このメソッドは、スレッドの状態を変更して、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
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照

Thread.Sleep メソッド



このページでは「.NET Framework クラス ライブラリ リファレンス」からThread.Sleepを検索した結果を表示しています。
Weblioに収録されているすべての辞書からThread.Sleepを検索する場合は、下記のリンクをクリックしてください。
 全ての辞書からThread.Sleep を検索

英和和英テキスト翻訳>> Weblio翻訳
英語⇒日本語日本語⇒英語
  

辞書ショートカット

すべての辞書の索引

「Thread.Sleep」の関連用語

Thread.Sleepのお隣キーワード
検索ランキング

   

英語⇒日本語
日本語⇒英語
   



Thread.Sleepのページの著作権
Weblio 辞書 情報提供元は 参加元一覧 にて確認できます。

   
日本マイクロソフト株式会社日本マイクロソフト株式会社
© 2025 Microsoft.All rights reserved.

©2025 GRAS Group, Inc.RSS