WaitHandle
WaitHandle
WaitHandle
WaitHandle
Class
定義
共有リソースへの排他アクセスの待機に使用するオペレーティング システム固有のオブジェクトをカプセル化します。
public ref class WaitHandle abstract : MarshalByRefObject, IDisposable
[System.Runtime.InteropServices.ComVisible(true)]
public abstract class WaitHandle : MarshalByRefObject, IDisposable
type WaitHandle = class
inherit MarshalByRefObject
interface IDisposable
Public MustInherit Class WaitHandle
Inherits MarshalByRefObject
Implements IDisposable
- 継承
-
WaitHandleWaitHandleWaitHandleWaitHandle
- 派生
-
System.Threading.AutoResetEventSystem.Threading.AutoResetEventSystem.Threading.AutoResetEventSystem.Threading.AutoResetEventSystem.Threading.EventWaitHandleSystem.Threading.EventWaitHandleSystem.Threading.EventWaitHandleSystem.Threading.EventWaitHandle
- 属性
- 実装
例
次のコード例に示す 2 つのスレッドができる方法は、メインの中にバック グラウンド タスク スレッド、静的なを使用して、タスクの待機WaitAnyとWaitAllのメソッド、WaitHandleクラス。
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).
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).
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).
注釈
WaitHandleクラスはネイティブのオペレーティング システムの同期ハンドルをカプセル化し、ランタイム内の複数の待機操作を許可するすべての同期オブジェクトを表すために使用します。 他の同期オブジェクトと待機ハンドルの比較は、次を参照してください。同期プリミティブの概要します。
WaitHandleクラス自体が抽象クラス。 派生したクラスWaitHandleを取得または解放の共有リソースへのアクセスを示すシグナリング機構を定義しますが、使用、継承されたWaitHandleへのアクセスの待機中にブロックするメソッドがリソースを共有します。 派生したクラスWaitHandleが含まれます。
EventWaitHandleクラスとその派生クラスでは、AutoResetEventとManualResetEventします。
Semaphore クラス 参照してくださいSemaphore と SemaphoreSlimします。
インスタンス メソッドを呼び出して個々 の待機ハンドルに対してスレッドをブロックできますWaitOneから派生したクラスによって継承WaitHandleします。
派生クラスのWaitHandleのスレッド アフィニティが異なります。 イベント待機ハンドル (EventWaitHandle、 AutoResetEvent、およびManualResetEvent) セマフォにはスレッド アフィニティがありません。 任意のスレッドは、イベント待機ハンドルまたはセマフォを通知できます。 ミュー テックス、一方ではスレッド アフィニティが;ミュー テックスを所有するスレッドを解放する必要があります、およびスレッドの呼び出し、例外がスローされますが、ReleaseMutexミュー テックスを所有していないメソッドです。
WaitHandleクラスから派生MarshalByRefObject、これらのクラスは、アプリケーション ドメイン境界を越えてスレッドのアクティビティを同期するために使用できます。
その派生クラスでだけでなく、WaitHandleクラスにはさまざまな 1 つまでスレッドをブロックする静的メソッドまたは複数の同期オブジェクトがシグナルを受信します。 次の設定があります。
SignalAndWait、スレッドの 1 つの待機ハンドルを通知し、すぐに別の待つことができます。
WaitAll、、スレッド、配列内のすべての待機ハンドルがシグナルを受信するまで待機することができます。
WaitAny、、スレッドの待機ハンドルの指定したセットのいずれかが通知されたまで待機することができます。
これらのメソッドのオーバー ロードは、待機、およびその他のスレッド同期コンテキストを使用できるように、待機に入る前に同期コンテキストを終了する機会を放棄するためのタイムアウト間隔を提供します。
重要
この型は、IDisposableインターフェイス。 型またはその派生型を使用してが完了したら、直接的または間接的にそれを破棄する必要があります。 型の直接 dispose を呼び出してそのCloseメソッド、 try
/ catch
ブロックします。 直接ことのない破棄する場合など、言語コンストラクトを使用してusing
(で C# の場合) またはUsing
(Visual Basic) でします。 詳細については、"を使用して、オブジェクトを実装する IDisposable"のセクションを参照してください、IDisposableインターフェイスに関するトピック。
WaitHandle 実装して、Disposeパターン。 参照してください Dispose メソッドの を実装します。 派生させた場合WaitHandleを使用して、SafeWaitHandleネイティブのオペレーティング システム ハンドルを格納するプロパティ。 保護されたをオーバーライドする必要はありませんDisposeメソッド追加されているアンマネージ リソースを使用する場合を除き、します。
コンストラクター
WaitHandle() WaitHandle() WaitHandle() WaitHandle() |
WaitHandle クラスの新しいインスタンスを初期化します。 |
フィールド
InvalidHandle InvalidHandle InvalidHandle InvalidHandle |
無効なネイティブ オペレーティング システム ハンドルを表します。 このフィールドは読み取り専用です。 |
WaitTimeout WaitTimeout WaitTimeout WaitTimeout |
待機ハンドルがシグナル状態になる前に WaitAny(WaitHandle[], Int32, Boolean) 操作がタイムアウトになったことを示します。 このフィールドは定数です。 |
プロパティ
Handle Handle Handle Handle |
ネイティブ オペレーティング システム ハンドルを取得または設定します。 |
SafeWaitHandle SafeWaitHandle SafeWaitHandle SafeWaitHandle |
ネイティブ オペレーティング システム ハンドルを取得または設定します。 |
メソッド
明示的なインターフェイスの実装
IDisposable.Dispose() IDisposable.Dispose() IDisposable.Dispose() IDisposable.Dispose() |
WaitHandle によって使用されているすべてのリソースを解放します。 |
Extension Methods
GetSafeWaitHandle(WaitHandle) GetSafeWaitHandle(WaitHandle) GetSafeWaitHandle(WaitHandle) GetSafeWaitHandle(WaitHandle) |
ネイティブ オペレーティング システムの待機ハンドルのためのセーフ ハンドルを取得します。 |
SetSafeWaitHandle(WaitHandle, SafeWaitHandle) SetSafeWaitHandle(WaitHandle, SafeWaitHandle) SetSafeWaitHandle(WaitHandle, SafeWaitHandle) SetSafeWaitHandle(WaitHandle, SafeWaitHandle) |
ネイティブ オペレーティング システムの待機ハンドルのためのセーフ ハンドルを設定します。 |
適用対象
スレッド セーフ
この型はスレッド セーフです。
こちらもご覧ください
フィードバック
お客様のご意見をお寄せください。 お寄せいただく内容の種類を選択:
このフィードバック システムは、GitHub Issues を利用して構築されています。 詳しくは、ブログをご覧ください。
フィードバックを読み込んでいます...