ManualResetEvent クラス
定義
重要
一部の情報は、リリース前に大きく変更される可能性があるプレリリースされた製品に関するものです。 Microsoft は、ここに記載されている情報について、明示または黙示を問わず、一切保証しません。
通知時に手動でリセットする必要のあるスレッド同期イベントを表します。 このクラスは継承できません。
public ref class ManualResetEvent sealed : System::Threading::EventWaitHandle
public ref class ManualResetEvent sealed : System::Threading::WaitHandle
public sealed class ManualResetEvent : System.Threading.EventWaitHandle
public sealed class ManualResetEvent : System.Threading.WaitHandle
[System.Runtime.InteropServices.ComVisible(true)]
public sealed class ManualResetEvent : System.Threading.EventWaitHandle
type ManualResetEvent = class
inherit EventWaitHandle
type ManualResetEvent = class
inherit WaitHandle
[<System.Runtime.InteropServices.ComVisible(true)>]
type ManualResetEvent = class
inherit EventWaitHandle
Public NotInheritable Class ManualResetEvent
Inherits EventWaitHandle
Public NotInheritable Class ManualResetEvent
Inherits WaitHandle
- 継承
- 継承
- 継承
- 属性
例
次の例では、のしくみを ManualResetEvent 示します。 この例は、符号なし状態の で始まります ManualResetEvent (つまり、 false
コンストラクターに渡されます)。 この例では、3 つのスレッドを作成します。各スレッドは、 メソッドを呼び出WaitOneして をManualResetEventブロックします。 ユーザーが Enter キーを押すと、 メソッドが Set 呼び出され、3 つのスレッドがすべて解放されます。 これは、スレッドを一度に 1 つずつ解放し、各リリース後に自動的にリセットする クラスの動作 AutoResetEvent と対照的です。
Enter キーをもう一度押すと、 メソッドがManualResetEvent呼び出されるまで Reset がシグナル状態のままになります。この例では、さらに 2 つのスレッドを開始します。 これらのスレッドは、 メソッドを呼び出すときに WaitOne ブロックするのではなく、完了まで実行します。
Enter キーをもう一度押すと、 メソッドが呼び出Resetされ、もう 1 つのスレッドが開始され、 が呼び出WaitOneされたときにブロックされます。 Enter キーを 1 回押すと、最後のスレッドが呼び出Setされて最後のスレッドが解放され、プログラムが終了します。
using namespace System;
using namespace System::Threading;
ref class Example
{
private:
// mre is used to block and release threads manually. It is
// created in the unsignaled state.
static ManualResetEvent^ mre = gcnew ManualResetEvent(false);
static void ThreadProc()
{
String^ name = Thread::CurrentThread->Name;
Console::WriteLine(name + " starts and calls mre->WaitOne()");
mre->WaitOne();
Console::WriteLine(name + " ends.");
}
public:
static void Demo()
{
Console::WriteLine("\nStart 3 named threads that block on a ManualResetEvent:\n");
for(int i = 0; i <=2 ; i++)
{
Thread^ t = gcnew Thread(gcnew ThreadStart(ThreadProc));
t->Name = "Thread_" + i;
t->Start();
}
Thread::Sleep(500);
Console::WriteLine("\nWhen all three threads have started, press Enter to call Set()" +
"\nto release all the threads.\n");
Console::ReadLine();
mre->Set();
Thread::Sleep(500);
Console::WriteLine("\nWhen a ManualResetEvent is signaled, threads that call WaitOne()" +
"\ndo not block. Press Enter to show this.\n");
Console::ReadLine();
for(int i = 3; i <= 4; i++)
{
Thread^ t = gcnew Thread(gcnew ThreadStart(ThreadProc));
t->Name = "Thread_" + i;
t->Start();
}
Thread::Sleep(500);
Console::WriteLine("\nPress Enter to call Reset(), so that threads once again block" +
"\nwhen they call WaitOne().\n");
Console::ReadLine();
mre->Reset();
// Start a thread that waits on the ManualResetEvent.
Thread^ t5 = gcnew Thread(gcnew ThreadStart(ThreadProc));
t5->Name = "Thread_5";
t5->Start();
Thread::Sleep(500);
Console::WriteLine("\nPress Enter to call Set() and conclude the demo.");
Console::ReadLine();
mre->Set();
// If you run this example in Visual Studio, uncomment the following line:
//Console::ReadLine();
}
};
int main()
{
Example::Demo();
}
/* This example produces output similar to the following:
Start 3 named threads that block on a ManualResetEvent:
Thread_0 starts and calls mre->WaitOne()
Thread_1 starts and calls mre->WaitOne()
Thread_2 starts and calls mre->WaitOne()
When all three threads have started, press Enter to call Set()
to release all the threads.
Thread_2 ends.
Thread_1 ends.
Thread_0 ends.
When a ManualResetEvent is signaled, threads that call WaitOne()
do not block. Press Enter to show this.
Thread_3 starts and calls mre->WaitOne()
Thread_3 ends.
Thread_4 starts and calls mre->WaitOne()
Thread_4 ends.
Press Enter to call Reset(), so that threads once again block
when they call WaitOne().
Thread_5 starts and calls mre->WaitOne()
Press Enter to call Set() and conclude the demo.
Thread_5 ends.
*/
using System;
using System.Threading;
public class Example
{
// mre is used to block and release threads manually. It is
// created in the unsignaled state.
private static ManualResetEvent mre = new ManualResetEvent(false);
static void Main()
{
Console.WriteLine("\nStart 3 named threads that block on a ManualResetEvent:\n");
for(int i = 0; i <= 2; i++)
{
Thread t = new Thread(ThreadProc);
t.Name = "Thread_" + i;
t.Start();
}
Thread.Sleep(500);
Console.WriteLine("\nWhen all three threads have started, press Enter to call Set()" +
"\nto release all the threads.\n");
Console.ReadLine();
mre.Set();
Thread.Sleep(500);
Console.WriteLine("\nWhen a ManualResetEvent is signaled, threads that call WaitOne()" +
"\ndo not block. Press Enter to show this.\n");
Console.ReadLine();
for(int i = 3; i <= 4; i++)
{
Thread t = new Thread(ThreadProc);
t.Name = "Thread_" + i;
t.Start();
}
Thread.Sleep(500);
Console.WriteLine("\nPress Enter to call Reset(), so that threads once again block" +
"\nwhen they call WaitOne().\n");
Console.ReadLine();
mre.Reset();
// Start a thread that waits on the ManualResetEvent.
Thread t5 = new Thread(ThreadProc);
t5.Name = "Thread_5";
t5.Start();
Thread.Sleep(500);
Console.WriteLine("\nPress Enter to call Set() and conclude the demo.");
Console.ReadLine();
mre.Set();
// If you run this example in Visual Studio, uncomment the following line:
//Console.ReadLine();
}
private static void ThreadProc()
{
string name = Thread.CurrentThread.Name;
Console.WriteLine(name + " starts and calls mre.WaitOne()");
mre.WaitOne();
Console.WriteLine(name + " ends.");
}
}
/* This example produces output similar to the following:
Start 3 named threads that block on a ManualResetEvent:
Thread_0 starts and calls mre.WaitOne()
Thread_1 starts and calls mre.WaitOne()
Thread_2 starts and calls mre.WaitOne()
When all three threads have started, press Enter to call Set()
to release all the threads.
Thread_2 ends.
Thread_0 ends.
Thread_1 ends.
When a ManualResetEvent is signaled, threads that call WaitOne()
do not block. Press Enter to show this.
Thread_3 starts and calls mre.WaitOne()
Thread_3 ends.
Thread_4 starts and calls mre.WaitOne()
Thread_4 ends.
Press Enter to call Reset(), so that threads once again block
when they call WaitOne().
Thread_5 starts and calls mre.WaitOne()
Press Enter to call Set() and conclude the demo.
Thread_5 ends.
*/
Imports System.Threading
Public Class Example
' mre is used to block and release threads manually. It is
' created in the unsignaled state.
Private Shared mre As New ManualResetEvent(False)
<MTAThreadAttribute> _
Shared Sub Main()
Console.WriteLine(vbLf & _
"Start 3 named threads that block on a ManualResetEvent:" & vbLf)
For i As Integer = 0 To 2
Dim t As New Thread(AddressOf ThreadProc)
t.Name = "Thread_" & i
t.Start()
Next i
Thread.Sleep(500)
Console.WriteLine(vbLf & _
"When all three threads have started, press Enter to call Set()" & vbLf & _
"to release all the threads." & vbLf)
Console.ReadLine()
mre.Set()
Thread.Sleep(500)
Console.WriteLine(vbLf & _
"When a ManualResetEvent is signaled, threads that call WaitOne()" & vbLf & _
"do not block. Press Enter to show this." & vbLf)
Console.ReadLine()
For i As Integer = 3 To 4
Dim t As New Thread(AddressOf ThreadProc)
t.Name = "Thread_" & i
t.Start()
Next i
Thread.Sleep(500)
Console.WriteLine(vbLf & _
"Press Enter to call Reset(), so that threads once again block" & vbLf & _
"when they call WaitOne()." & vbLf)
Console.ReadLine()
mre.Reset()
' Start a thread that waits on the ManualResetEvent.
Dim t5 As New Thread(AddressOf ThreadProc)
t5.Name = "Thread_5"
t5.Start()
Thread.Sleep(500)
Console.WriteLine(vbLf & "Press Enter to call Set() and conclude the demo.")
Console.ReadLine()
mre.Set()
' If you run this example in Visual Studio, uncomment the following line:
'Console.ReadLine()
End Sub
Private Shared Sub ThreadProc()
Dim name As String = Thread.CurrentThread.Name
Console.WriteLine(name & " starts and calls mre.WaitOne()")
mre.WaitOne()
Console.WriteLine(name & " ends.")
End Sub
End Class
' This example produces output similar to the following:
'
'Start 3 named threads that block on a ManualResetEvent:
'
'Thread_0 starts and calls mre.WaitOne()
'Thread_1 starts and calls mre.WaitOne()
'Thread_2 starts and calls mre.WaitOne()
'
'When all three threads have started, press Enter to call Set()
'to release all the threads.
'
'
'Thread_2 ends.
'Thread_0 ends.
'Thread_1 ends.
'
'When a ManualResetEvent is signaled, threads that call WaitOne()
'do not block. Press Enter to show this.
'
'
'Thread_3 starts and calls mre.WaitOne()
'Thread_3 ends.
'Thread_4 starts and calls mre.WaitOne()
'Thread_4 ends.
'
'Press Enter to call Reset(), so that threads once again block
'when they call WaitOne().
'
'
'Thread_5 starts and calls mre.WaitOne()
'
'Press Enter to call Set() and conclude the demo.
'
'Thread_5 ends.
注釈
スレッドの相互作用 (またはスレッド シグナリング) には、AutoResetEventおよび EventWaitHandle を使用ManualResetEvent
します。 詳細については、同期プリミティブの概要に関する記事の スレッド操作またはシグナル通知 に 関するセクションを 参照してください。
スレッドは、他のスレッドが続行する前に完了する必要があるアクティビティを開始すると、 ManualResetEvent.Reset を呼び出して非シグナル状態になります ManualResetEvent
。 このスレッドは、 を制御すると ManualResetEvent
考えることができます。 シグナルを待機している ManualResetEvent.WaitOne ブロックを呼び出すスレッド。 制御スレッドがアクティビティを完了すると、 ManualResetEvent.Set を呼び出して、待機中のスレッドが続行できることを通知します。 待機中のすべてのスレッドが解放されます。
シグナルが通知されると、 ManualResetEvent
メソッドを呼び出して手動でリセットされるまで、シグナル状態が Reset() 維持されます。 つまり、 を呼び出して WaitOne すぐにを返します。
の初期状態 ManualResetEvent
を制御するには、ブール値をコンストラクターに渡します true
。初期状態が通知される場合は 、 false
それ以外の場合は 。
ManualResetEvent
および メソッドと共にstatic
WaitAllWaitAny使用することもできます。
.NET Framework バージョン 2.0 以降では、 ManualResetEvent クラスからEventWaitHandle派生します。 はManualResetEvent、 で作成された EventResetMode.ManualResetとEventWaitHandle機能的に同じです。
注意
ManualResetEventクラスとは異なり、 クラスはEventWaitHandle名前付きシステム同期イベントへのアクセスを提供します。
.NET Framework バージョン 4.0 以降ではSystem.Threading.ManualResetEventSlim、 クラスは にManualResetEvent代わる軽量です。
コンストラクター
ManualResetEvent(Boolean) |
初期状態をシグナル状態に設定するかどうかを示す Boolean 型の値を使用して、ManualResetEvent クラスの新しいインスタンスを初期化します。 |
フィールド
WaitTimeout |
待機ハンドルがシグナル状態になる前に WaitAny(WaitHandle[], Int32, Boolean) 操作がタイムアウトになったことを示します。 このフィールドは定数です。 (継承元 WaitHandle) |
プロパティ
Handle |
互換性のために残されています。
互換性のために残されています。
ネイティブ オペレーティング システム ハンドルを取得または設定します。 (継承元 WaitHandle) |
SafeWaitHandle |
ネイティブ オペレーティング システム ハンドルを取得または設定します。 (継承元 WaitHandle) |
メソッド
Close() |
現在の WaitHandle によって保持されているすべてのリソースを解放します。 (継承元 WaitHandle) |
CreateObjRef(Type) |
リモート オブジェクトとの通信に使用するプロキシの生成に必要な情報をすべて格納しているオブジェクトを作成します。 (継承元 MarshalByRefObject) |
Dispose() |
WaitHandle クラスの現在のインスタンスによって使用されているすべてのリソースを解放します。 (継承元 WaitHandle) |
Dispose(Boolean) |
派生クラスでオーバーライドされると、WaitHandle によって使用されているアンマネージド リソースを解放し、オプションでマネージド リソースも解放します。 (継承元 WaitHandle) |
Equals(Object) |
指定されたオブジェクトが現在のオブジェクトと等しいかどうかを判断します。 (継承元 Object) |
GetAccessControl() |
現在の EventWaitHandle オブジェクトによって表される名前付きシステム イベントのアクセス制御セキュリティを表す EventWaitHandleSecurity オブジェクトを取得します。 (継承元 EventWaitHandle) |
GetHashCode() |
既定のハッシュ関数として機能します。 (継承元 Object) |
GetLifetimeService() |
互換性のために残されています。
対象のインスタンスの有効期間ポリシーを制御する、現在の有効期間サービス オブジェクトを取得します。 (継承元 MarshalByRefObject) |
GetType() |
現在のインスタンスの Type を取得します。 (継承元 Object) |
InitializeLifetimeService() |
互換性のために残されています。
このインスタンスの有効期間ポリシーを制御する有効期間サービス オブジェクトを取得します。 (継承元 MarshalByRefObject) |
MemberwiseClone() |
現在の Object の簡易コピーを作成します。 (継承元 Object) |
MemberwiseClone(Boolean) |
現在の MarshalByRefObject オブジェクトの簡易コピーを作成します。 (継承元 MarshalByRefObject) |
Reset() |
イベントの状態を非シグナル状態に設定し、スレッドをブロックします。 |
Reset() |
イベントの状態を非シグナル状態に設定し、スレッドをブロックします。 (継承元 EventWaitHandle) |
Set() |
イベントの状態をシグナル状態に設定し、待機している 1 つ以上のスレッドが進行できるようにします。 |
Set() |
イベントの状態をシグナル状態に設定し、待機している 1 つ以上のスレッドが進行できるようにします。 (継承元 EventWaitHandle) |
SetAccessControl(EventWaitHandleSecurity) |
名前付きシステム イベントのアクセス制御セキュリティを設定します。 (継承元 EventWaitHandle) |
ToString() |
現在のオブジェクトを表す文字列を返します。 (継承元 Object) |
WaitOne() |
現在の WaitHandle がシグナルを受け取るまで、現在のスレッドをブロックします。 (継承元 WaitHandle) |
WaitOne(Int32) |
32 ビット符号付き整数を使用して時間間隔をミリ秒単位で指定し、現在の WaitHandle がシグナルを受信するまで、現在のスレッドをブロックします。 (継承元 WaitHandle) |
WaitOne(Int32, Boolean) |
現在の WaitHandle がシグナルを受信するまで現在のスレッドをブロックします。時間間隔を指定するために 32 ビット符号付き整数を使用し、待機の前でも同期ドメインを終了するかどうかを指定します。 (継承元 WaitHandle) |
WaitOne(TimeSpan) |
TimeSpan を使用して時間間隔を指定し、現在のインスタンスがシグナルを受信するまで現在のスレッドをブロックします。 (継承元 WaitHandle) |
WaitOne(TimeSpan, Boolean) |
現在のインスタンスがシグナルを受信するまで現在のスレッドをブロックします。TimeSpan を使用して時間間隔を指定し、待機の前でも同期ドメインを終了するかどうかを指定します。 (継承元 WaitHandle) |
明示的なインターフェイスの実装
IDisposable.Dispose() |
この API は製品インフラストラクチャをサポートします。コードから直接使用するものではありません。 WaitHandle によって使用されているすべてのリソースを解放します。 (継承元 WaitHandle) |
拡張メソッド
GetAccessControl(EventWaitHandle) |
指定した |
SetAccessControl(EventWaitHandle, EventWaitHandleSecurity) |
指定したイベント待機ハンドルのセキュリティ記述子を設定します。 |
GetSafeWaitHandle(WaitHandle) |
ネイティブ オペレーティング システムの待機ハンドルのためのセーフ ハンドルを取得します。 |
SetSafeWaitHandle(WaitHandle, SafeWaitHandle) |
ネイティブ オペレーティング システムの待機ハンドルのためのセーフ ハンドルを設定します。 |
適用対象
スレッド セーフ
このクラスはスレッド セーフです。