WaitHandleとは? わかりやすく解説

WaitHandle クラス

共有リソースへの排他アクセス待機使用するオペレーティング システム固有のオブジェクトカプセル化ます。

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

<ComVisibleAttribute(True)> _
Public MustInherit Class
 WaitHandle
    Inherits MarshalByRefObject
    Implements IDisposable
[ComVisibleAttribute(true)] 
public abstract class WaitHandle : MarshalByRefObject,
 IDisposable
[ComVisibleAttribute(true)] 
public ref class WaitHandle abstract : public
 MarshalByRefObject, IDisposable
/** @attribute ComVisibleAttribute(true) */ 
public abstract class WaitHandle extends MarshalByRefObject
 implements IDisposable
ComVisibleAttribute(true) 
public abstract class WaitHandle extends
 MarshalByRefObject implements IDisposable
解説解説
使用例使用例

2 つスレッドバックグラウンド タスク実行しその間メイン スレッドWaitHandle クラスの WaitAny 静的メソッドおよび WaitAll 静的メソッド使用してタスク完了待機できるようにするコード例次に示します

Imports System
Imports System.Threading

NotInheritable Public Class
 App
    ' Define an array with two AutoResetEvent WaitHandles.
    Private Shared waitHandles() As
 WaitHandle = _
        {New AutoResetEvent(False), New AutoResetEvent(False)}
    
    ' Define a random number generator for testing.
    Private Shared r As
 New Random()
    
    <MTAThreadAttribute> _
    Public Shared Sub Main()
 
        ' Queue two tasks on two different threads; 
        ' wait until all tasks are completed.
        Dim dt As DateTime = DateTime.Now
        Console.WriteLine("Main thread is waiting for BOTH tasks
 to complete.")
        ThreadPool.QueueUserWorkItem(AddressOf DoTask, waitHandles(0))
        ThreadPool.QueueUserWorkItem(AddressOf DoTask, waitHandles(1))
        WaitHandle.WaitAll(waitHandles)
        ' The time shown below should match the longest task.
        Console.WriteLine("Both tasks are completed (time waited={0})",
 _
            (DateTime.Now - dt).TotalMilliseconds)
        
        ' Queue up two tasks on two different threads; 
        ' wait until any tasks are completed.
        dt = DateTime.Now
        Console.WriteLine()
        Console.WriteLine("The main thread is waiting for either
 task to complete.")
        ThreadPool.QueueUserWorkItem(AddressOf DoTask, waitHandles(0))
        ThreadPool.QueueUserWorkItem(AddressOf DoTask, waitHandles(1))
        Dim index As Integer
 = WaitHandle.WaitAny(waitHandles)
        ' The time shown below should match the shortest task.
        Console.WriteLine("Task {0} finished first (time waited={1}).",
 _
            index + 1,(DateTime.Now - dt).TotalMilliseconds)
    
    End Sub 'Main
    
    Shared Sub DoTask(ByVal
 state As [Object]) 
        Dim are As AutoResetEvent = CType(state,
 AutoResetEvent)
        Dim time As Integer
 = 1000 * r.Next(2, 10)
        Console.WriteLine("Performing a task for {0} milliseconds.",
 time)
        Thread.Sleep(time)
        are.Set()
    
    End Sub 'DoTask
End Class 'App

' This code produces output similar to the following:
'
'  Main thread is waiting for BOTH tasks to complete.
'  Performing a task for 7000 milliseconds.
'  Performing a task for 4000 milliseconds.
'  Both tasks are completed (time waited=7064.8052)
' 
'  The main thread is waiting for either task to complete.
'  Performing a task for 2000 milliseconds.
'  Performing a task for 2000 milliseconds.
'  Task 1 finished first (time waited=2000.6528).
using System;
using System.Threading;

public sealed class App 
{
    // Define an array with two AutoResetEvent WaitHandles.
    static WaitHandle[] waitHandles = new WaitHandle[]
 
    {
        new AutoResetEvent(false),
        new AutoResetEvent(false)
    };

    // Define a random number generator for testing.
    static Random r = new Random();

    static void Main() 
    {
        // Queue up two tasks on two different threads; 
        // wait until all tasks are completed.
        DateTime dt = DateTime.Now;
        Console.WriteLine("Main thread is waiting for BOTH
 tasks to complete.");
        ThreadPool.QueueUserWorkItem(new WaitCallback(DoTask),
 waitHandles[0]);
        ThreadPool.QueueUserWorkItem(new WaitCallback(DoTask),
 waitHandles[1]);
        WaitHandle.WaitAll(waitHandles);
        // The time shown below should match the longest task.
        Console.WriteLine("Both tasks are completed (time waited={0})",
 
            (DateTime.Now - dt).TotalMilliseconds);

        // Queue up two tasks on two different threads; 
        // wait until any tasks are completed.
        dt = DateTime.Now;
        Console.WriteLine();
        Console.WriteLine("The main thread is waiting for
 either task to complete.");
        ThreadPool.QueueUserWorkItem(new WaitCallback(DoTask),
 waitHandles[0]);
        ThreadPool.QueueUserWorkItem(new WaitCallback(DoTask),
 waitHandles[1]);
        int index = WaitHandle.WaitAny(waitHandles);
        // The time shown below should match the shortest task.
        Console.WriteLine("Task {0} finished first (time waited={1})."
,
            index + 1, (DateTime.Now - dt).TotalMilliseconds);
    }

    static void DoTask(Object state) 
    {
        AutoResetEvent are = (AutoResetEvent) state;
        int time = 1000 * r.Next(2, 10);
        Console.WriteLine("Performing a task for {0} milliseconds.",
 time);
        Thread.Sleep(time);
        are.Set();
    }
}

// This code produces output similar to the following:
//
//  Main thread is waiting for BOTH tasks to complete.
//  Performing a task for 7000 milliseconds.
//  Performing a task for 4000 milliseconds.
//  Both tasks are completed (time waited=7064.8052)
// 
//  The main thread is waiting for either task to complete.
//  Performing a task for 2000 milliseconds.
//  Performing a task for 2000 milliseconds.
//  Task 1 finished first (time waited=2000.6528).
using namespace System;
using namespace System::Threading;

public ref class WaitHandleExample
{
    // Define a random number generator for testing.
private:
    static Random^ random = gcnew Random();
public:
    static void DoTask(Object^ state)
    {
        AutoResetEvent^ autoReset = (AutoResetEvent^) state;
        int time = 1000 * random->Next(2, 10);
        Console::WriteLine("Performing a task for {0} milliseconds.",
 time);
        Thread::Sleep(time);
        autoReset->Set();
    }
};

int main()
{
    // Define an array with two AutoResetEvent WaitHandles.
    array<WaitHandle^>^ handles = gcnew array<WaitHandle^> {
        gcnew AutoResetEvent(false), gcnew AutoResetEvent(false)};

    // Queue up two tasks on two different threads;
    // wait until all tasks are completed.
    DateTime timeInstance = DateTime::Now;
    Console::WriteLine("Main thread is waiting for BOTH tasks
 to " +
        "complete.");
    ThreadPool::QueueUserWorkItem(
        gcnew WaitCallback(WaitHandleExample::DoTask), handles[0]);
    ThreadPool::QueueUserWorkItem(
        gcnew WaitCallback(WaitHandleExample::DoTask), handles[1]);
    WaitHandle::WaitAll(handles);
    // The time shown below should match the longest task.
    Console::WriteLine("Both tasks are completed (time waited={0})",
        (DateTime::Now - timeInstance).TotalMilliseconds);

    // Queue up two tasks on two different threads;
    // wait until any tasks are completed.
    timeInstance = DateTime::Now;
    Console::WriteLine();
    Console::WriteLine("The main thread is waiting for either
 task to " +
        "complete.");
    ThreadPool::QueueUserWorkItem(
        gcnew WaitCallback(WaitHandleExample::DoTask), handles[0]);
    ThreadPool::QueueUserWorkItem(
        gcnew WaitCallback(WaitHandleExample::DoTask), handles[1]);
    int index = WaitHandle::WaitAny(handles);
    // The time shown below should match the shortest task.
    Console::WriteLine("Task {0} finished first (time waited={1}).",
        index + 1, (DateTime::Now - timeInstance).TotalMilliseconds);
}

// This code produces the following sample output.
//
// Main thread is waiting for BOTH tasks to complete.
// Performing a task for 7000 milliseconds.
// Performing a task for 4000 milliseconds.
// Both tasks are completed (time waited=7064.8052)

// The main thread is waiting for either task to complete.
// Performing a task for 2000 milliseconds.
// Performing a task for 2000 milliseconds.
// Task 1 finished first (time waited=2000.6528).
継承階層継承階層
System.Object
   System.MarshalByRefObject
    System.Threading.WaitHandle
       System.Threading.EventWaitHandle
       System.Threading.Mutex
       System.Threading.Semaphore
スレッド セーフスレッド セーフ

この型は、スレッド セーフです。

プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照

WaitHandle コンストラクタ


WaitHandle フィールド


WaitHandle プロパティ


WaitHandle メソッド


パブリック メソッドパブリック メソッド

( プロテクト メソッド参照)
  名前 説明
パブリック メソッド Close 派生クラスオーバーライドされると、現在の WaitHandle で保持されているすべてのリソース解放します。
パブリック メソッド CreateObjRef  リモート オブジェクトとの通信使用するプロキシ生成必要な情報をすべて格納しているオブジェクト作成します。 ( MarshalByRefObject から継承されます。)
パブリック メソッド Equals  オーバーロードされます2 つObject インスタンス等しかどうか判断します。 ( Object から継承されます。)
パブリック メソッド GetHashCode  特定の型のハッシュ関数として機能します。GetHashCode は、ハッシュ アルゴリズムや、ハッシュ テーブルのようなデータ構造での使用適してます。 ( Object から継承されます。)
パブリック メソッド GetLifetimeService  対象インスタンス有効期間ポリシー制御する現在の有効期間サービス オブジェクト取得します。 ( MarshalByRefObject から継承されます。)
パブリック メソッド GetType  現在のインスタンスType取得します。 ( Object から継承されます。)
パブリック メソッド InitializeLifetimeService  対象インスタンス有効期間ポリシー制御する有効期間サービス オブジェクト取得します。 ( MarshalByRefObject から継承されます。)
パブリック メソッド ReferenceEquals  指定した複数Object インスタンス同一かどうか判断します。 ( Object から継承されます。)
パブリック メソッド SignalAndWait オーバーロードされます分割不可能な操作として1 つWaitHandle通知し別の WaitHandle待機します。
パブリック メソッド ToString  現在の Object を表す String返します。 ( Object から継承されます。)
パブリック メソッド WaitAll オーバーロードされます指定した配列内のすべての要素シグナル受信するまで待機します。
パブリック メソッド WaitAny オーバーロードされます指定した配列内のいずれか要素シグナル受信するまで待機します。
パブリック メソッド WaitOne オーバーロードされます派生クラスオーバーライドされると、現在の WaitHandleシグナル受信するまで現在のスレッドブロックします
プロテクト メソッドプロテクト メソッド
明示的インターフェイスの実装明示的インターフェイス実装
  名前 説明
インターフェイスの明示的な実装 System.IDisposable.Dispose WaitHandle によって使用されているすべてのリソース解放します。
参照参照

関連項目

WaitHandle クラス
System.Threading 名前空間

その他の技術情報

待機ハンドル

WaitHandle メンバ

共有リソースへの排他アクセス待機使用するオペレーティング システム固有のオブジェクトカプセル化ます。

WaitHandle データ型公開されるメンバを以下の表に示します


プロテクト コンストラクタプロテクト コンストラクタ
  名前 説明
プロテクト メソッド WaitHandle WaitHandle クラス新しインスタンス初期化します。
パブリック フィールドパブリック フィールド
プロテクト フィールドプロテクト フィールド
パブリック プロパティパブリック プロパティ
パブリック メソッドパブリック メソッド
( プロテクト メソッド参照)
  名前 説明
パブリック メソッド Close 派生クラスオーバーライドされると、現在の WaitHandle で保持されているすべてのリソース解放します。
パブリック メソッド CreateObjRef  リモート オブジェクトとの通信使用するプロキシ生成必要な情報をすべて格納しているオブジェクト作成します。 (MarshalByRefObject から継承されます。)
パブリック メソッド Equals  オーバーロードされます2 つObject インスタンス等しかどうか判断します。 (Object から継承されます。)
パブリック メソッド GetHashCode  特定の型のハッシュ関数として機能します。GetHashCode は、ハッシュ アルゴリズムや、ハッシュ テーブルのようなデータ構造での使用適してます。 (Object から継承されます。)
パブリック メソッド GetLifetimeService  対象インスタンス有効期間ポリシー制御する現在の有効期間サービス オブジェクト取得します。 (MarshalByRefObject から継承されます。)
パブリック メソッド GetType  現在のインスタンスType取得します。 (Object から継承されます。)
パブリック メソッド InitializeLifetimeService  対象インスタンス有効期間ポリシー制御する有効期間サービス オブジェクト取得します。 (MarshalByRefObject から継承されます。)
パブリック メソッド ReferenceEquals  指定した複数Object インスタンス同一かどうか判断します。 (Object から継承されます。)
パブリック メソッド SignalAndWait オーバーロードされます分割不可能な操作として1 つWaitHandle通知し別の WaitHandle待機します。
パブリック メソッド ToString  現在の Object を表す String返します。 (Object から継承されます。)
パブリック メソッド WaitAll オーバーロードされます指定した配列内のすべての要素シグナル受信するまで待機します。
パブリック メソッド WaitAny オーバーロードされます指定した配列内のいずれか要素シグナル受信するまで待機します。
パブリック メソッド WaitOne オーバーロードされます派生クラスオーバーライドされると、現在の WaitHandleシグナル受信するまで現在のスレッドブロックします
プロテクト メソッドプロテクト メソッド
明示的インターフェイスの実装明示的インターフェイス実装
  名前 説明
インターフェイスの明示的な実装 System.IDisposable.Dispose WaitHandle によって使用されているすべてのリソース解放します。
参照参照

関連項目

WaitHandle クラス
System.Threading 名前空間

その他の技術情報

待機ハンドル



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

辞書ショートカット

すべての辞書の索引

「WaitHandle」の関連用語

WaitHandleのお隣キーワード
検索ランキング

   

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



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

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

©2024 GRAS Group, Inc.RSS