AutoResetEvent AutoResetEvent AutoResetEvent AutoResetEvent Class

定義

通知を受けたときに、単一の待機中のスレッドを解放した後、自動的にリセットされるスレッドの同期イベントを表します。Represents a thread synchronization event that, when signaled, resets automatically after releasing a single waiting thread. このクラスは継承できません。This class cannot be inherited.

public ref class AutoResetEvent sealed : System::Threading::EventWaitHandle
[System.Runtime.InteropServices.ComVisible(true)]
public sealed class AutoResetEvent : System.Threading.EventWaitHandle
type AutoResetEvent = class
    inherit EventWaitHandle
Public NotInheritable Class AutoResetEvent
Inherits EventWaitHandle
継承
属性

次の例は、使用する方法を示しますAutoResetEvent呼び出すことによって、一度に 1 つのスレッドを解放する、Setメソッド (基底クラス) の各時間のユーザーが、 Enterキー。The following example shows how to use AutoResetEvent to release one thread at a time, by calling the Set method (on the base class) each time the user presses the Enter key. 例では、3 つのスレッドで待機を開始する、AutoResetEventシグナル状態で作成しました。The example starts three threads, which wait on an AutoResetEvent that was created in the signaled state. 最初のスレッドはため、すぐに解放、AutoResetEventが既にシグナル状態にします。The first thread is released immediately, because the AutoResetEvent is already in the signaled state. これにより、リセット、AutoResetEvent非シグナル状態にその後のスレッドをブロックするようです。This resets the AutoResetEvent to the non-signaled state, so that subsequent threads block. ブロックされたスレッドは、ユーザーを解放するまでに 1 つは解放されませんキーを押して一度に、 Enterキー。The blocked threads are not released until the user releases them one at a time by pressing the Enter key.

スレッドは最初のリリース後AutoResetEvent、別の待つ、AutoResetEvent非シグナル状態で作成しました。After the threads are released from the first AutoResetEvent, they wait on another AutoResetEvent that was created in the non-signaled state. 次の 3 つのすべてのスレッドをブロック、そのため、Setメソッドは、すべてリリースを 3 回呼び出す必要があります。All three threads block, so the Set method must be called three times to release them all.

using namespace System;
using namespace System::Threading;

ref class Example
{
private:
    static AutoResetEvent^ event_1 = gcnew AutoResetEvent(true);
    static AutoResetEvent^ event_2 = gcnew AutoResetEvent(false);

    static void ThreadProc()
    {
        String^ name = Thread::CurrentThread->Name;

        Console::WriteLine("{0} waits on AutoResetEvent #1.", name);
        event_1->WaitOne();
        Console::WriteLine("{0} is released from AutoResetEvent #1.", name);

        Console::WriteLine("{0} waits on AutoResetEvent #2.", name);
        event_2->WaitOne();
        Console::WriteLine("{0} is released from AutoResetEvent #2.", name);

        Console::WriteLine("{0} ends.", name);
    }

public:
    static void Demo()
    {
        Console::WriteLine("Press Enter to create three threads and start them.\r\n" +
                           "The threads wait on AutoResetEvent #1, which was created\r\n" +
                           "in the signaled state, so the first thread is released.\r\n" +
                           "This puts AutoResetEvent #1 into the unsignaled state.");
        Console::ReadLine();
            
        for (int i = 1; i < 4; i++)
        {
            Thread^ t = gcnew Thread(gcnew ThreadStart(&ThreadProc));
            t->Name = "Thread_" + i;
            t->Start();
        }
        Thread::Sleep(250);

        for (int i = 0; i < 2; i++)
        {
            Console::WriteLine("Press Enter to release another thread.");
            Console::ReadLine();
            event_1->Set();
            Thread::Sleep(250);
        }

        Console::WriteLine("\r\nAll threads are now waiting on AutoResetEvent #2.");
        for (int i = 0; i < 3; i++)
        {
            Console::WriteLine("Press Enter to release a thread.");
            Console::ReadLine();
            event_2->Set();
            Thread::Sleep(250);
        }

        // Visual Studio: Uncomment the following line.
        //Console::Readline();
    }
};

void main()
{
    Example::Demo();
}

/* This example produces output similar to the following:

Press Enter to create three threads and start them.
The threads wait on AutoResetEvent #1, which was created
in the signaled state, so the first thread is released.
This puts AutoResetEvent #1 into the unsignaled state.

Thread_1 waits on AutoResetEvent #1.
Thread_1 is released from AutoResetEvent #1.
Thread_1 waits on AutoResetEvent #2.
Thread_3 waits on AutoResetEvent #1.
Thread_2 waits on AutoResetEvent #1.
Press Enter to release another thread.

Thread_3 is released from AutoResetEvent #1.
Thread_3 waits on AutoResetEvent #2.
Press Enter to release another thread.

Thread_2 is released from AutoResetEvent #1.
Thread_2 waits on AutoResetEvent #2.

All threads are now waiting on AutoResetEvent #2.
Press Enter to release a thread.

Thread_2 is released from AutoResetEvent #2.
Thread_2 ends.
Press Enter to release a thread.

Thread_1 is released from AutoResetEvent #2.
Thread_1 ends.
Press Enter to release a thread.

Thread_3 is released from AutoResetEvent #2.
Thread_3 ends.
 */
using System;
using System.Threading;

// Visual Studio: Replace the default class in a Console project with 
//                the following class.
class Example
{
    private static AutoResetEvent event_1 = new AutoResetEvent(true);
    private static AutoResetEvent event_2 = new AutoResetEvent(false);

    static void Main()
    {
        Console.WriteLine("Press Enter to create three threads and start them.\r\n" +
                          "The threads wait on AutoResetEvent #1, which was created\r\n" +
                          "in the signaled state, so the first thread is released.\r\n" +
                          "This puts AutoResetEvent #1 into the unsignaled state.");
        Console.ReadLine();
            
        for (int i = 1; i < 4; i++)
        {
            Thread t = new Thread(ThreadProc);
            t.Name = "Thread_" + i;
            t.Start();
        }
        Thread.Sleep(250);

        for (int i = 0; i < 2; i++)
        {
            Console.WriteLine("Press Enter to release another thread.");
            Console.ReadLine();
            event_1.Set();
            Thread.Sleep(250);
        }

        Console.WriteLine("\r\nAll threads are now waiting on AutoResetEvent #2.");
        for (int i = 0; i < 3; i++)
        {
            Console.WriteLine("Press Enter to release a thread.");
            Console.ReadLine();
            event_2.Set();
            Thread.Sleep(250);
        }

        // Visual Studio: Uncomment the following line.
        //Console.Readline();
    }

    static void ThreadProc()
    {
        string name = Thread.CurrentThread.Name;

        Console.WriteLine("{0} waits on AutoResetEvent #1.", name);
        event_1.WaitOne();
        Console.WriteLine("{0} is released from AutoResetEvent #1.", name);

        Console.WriteLine("{0} waits on AutoResetEvent #2.", name);
        event_2.WaitOne();
        Console.WriteLine("{0} is released from AutoResetEvent #2.", name);

        Console.WriteLine("{0} ends.", name);
    }
}

/* This example produces output similar to the following:

Press Enter to create three threads and start them.
The threads wait on AutoResetEvent #1, which was created
in the signaled state, so the first thread is released.
This puts AutoResetEvent #1 into the unsignaled state.

Thread_1 waits on AutoResetEvent #1.
Thread_1 is released from AutoResetEvent #1.
Thread_1 waits on AutoResetEvent #2.
Thread_3 waits on AutoResetEvent #1.
Thread_2 waits on AutoResetEvent #1.
Press Enter to release another thread.

Thread_3 is released from AutoResetEvent #1.
Thread_3 waits on AutoResetEvent #2.
Press Enter to release another thread.

Thread_2 is released from AutoResetEvent #1.
Thread_2 waits on AutoResetEvent #2.

All threads are now waiting on AutoResetEvent #2.
Press Enter to release a thread.

Thread_2 is released from AutoResetEvent #2.
Thread_2 ends.
Press Enter to release a thread.

Thread_1 is released from AutoResetEvent #2.
Thread_1 ends.
Press Enter to release a thread.

Thread_3 is released from AutoResetEvent #2.
Thread_3 ends.
 */
Imports System.Threading

' Visual Studio: Replace the default class in a Console project with 
'                the following class.
Class Example

    Private Shared event_1 As New AutoResetEvent(True)
    Private Shared event_2 As New AutoResetEvent(False)

    <MTAThread()> _
    Shared Sub Main()
    
        Console.WriteLine("Press Enter to create three threads and start them." & vbCrLf & _
                          "The threads wait on AutoResetEvent #1, which was created" & vbCrLf & _
                          "in the signaled state, so the first thread is released." & vbCrLf & _
                          "This puts AutoResetEvent #1 into the unsignaled state.")
        Console.ReadLine()
            
        For i As Integer = 1 To 3
            Dim t As New Thread(AddressOf ThreadProc)
            t.Name = "Thread_" & i
            t.Start()
        Next
        Thread.Sleep(250)

        For i As Integer = 1 To 2
            Console.WriteLine("Press Enter to release another thread.")
            Console.ReadLine()

            event_1.Set()
            Thread.Sleep(250)
        Next

        Console.WriteLine(vbCrLf & "All threads are now waiting on AutoResetEvent #2.")
        For i As Integer = 1 To 3
            Console.WriteLine("Press Enter to release a thread.")
            Console.ReadLine()

            event_2.Set()
            Thread.Sleep(250)
        Next

        ' Visual Studio: Uncomment the following line.
        'Console.Readline()
    End Sub

    Shared Sub ThreadProc()
    
        Dim name As String = Thread.CurrentThread.Name

        Console.WriteLine("{0} waits on AutoResetEvent #1.", name)
        event_1.WaitOne()
        Console.WriteLine("{0} is released from AutoResetEvent #1.", name)

        Console.WriteLine("{0} waits on AutoResetEvent #2.", name)
        event_2.WaitOne()
        Console.WriteLine("{0} is released from AutoResetEvent #2.", name)

        Console.WriteLine("{0} ends.", name)
    End Sub
End Class

' This example produces output similar to the following:
'
'Press Enter to create three threads and start them.
'The threads wait on AutoResetEvent #1, which was created
'in the signaled state, so the first thread is released.
'This puts AutoResetEvent #1 into the unsignaled state.
'
'Thread_1 waits on AutoResetEvent #1.
'Thread_1 is released from AutoResetEvent #1.
'Thread_1 waits on AutoResetEvent #2.
'Thread_3 waits on AutoResetEvent #1.
'Thread_2 waits on AutoResetEvent #1.
'Press Enter to release another thread.
'
'Thread_3 is released from AutoResetEvent #1.
'Thread_3 waits on AutoResetEvent #2.
'Press Enter to release another thread.
'
'Thread_2 is released from AutoResetEvent #1.
'Thread_2 waits on AutoResetEvent #2.
'
'All threads are now waiting on AutoResetEvent #2.
'Press Enter to release a thread.
'
'Thread_2 is released from AutoResetEvent #2.
'Thread_2 ends.
'Press Enter to release a thread.
'
'Thread_1 is released from AutoResetEvent #2.
'Thread_1 ends.
'Press Enter to release a thread.
'
'Thread_3 is released from AutoResetEvent #2.
'Thread_3 ends.

注釈

使用するAutoResetEventManualResetEvent、およびEventWaitHandleスレッドの相互作用 (またはスレッドがシグナル通知)。You use AutoResetEvent, ManualResetEvent, and EventWaitHandle for thread interaction (or thread signaling). 詳細については、次を参照してください。、スレッドの相互作用、またはシグナルのセクション、同期プリミティブの概要記事。For more information, see the Thread interaction, or signaling section of the Overview of synchronization primitives article.

重要

この型は IDisposable インターフェイスを実装します。This type implements the IDisposable interface. 型の使用が完了したら、直接的または間接的に型を破棄する必要があります。When you have finished using the type, you should dispose of it either directly or indirectly. 直接的に型を破棄するには、try / catch ブロック内で Dispose メソッドを呼び出します。To dispose of the type directly, call its Dispose method in a try/catch block. 間接的に型を破棄するには、using (C# の場合) または Using (Visual Basic 言語) などの言語構成要素を使用します。To dispose of it indirectly, use a language construct such as using (in C#) or Using (in Visual Basic). 詳細については、IDisposable インターフェイスに関するトピック内の「IDisposable を実装するオブジェクトの使用」セクションを参照してください。For more information, see the "Using an Object that Implements IDisposable" section in the IDisposable interface topic.

呼び出すことによって、スレッドがシグナルの待機AutoResetEvent.WaitOneします。A thread waits for a signal by calling AutoResetEvent.WaitOne. 場合、 AutoResetEvent 、スレッドがブロックされるまで、非シグナル状態にあるAutoResetEvent.Setが呼び出されます。If the AutoResetEvent is in the non-signaled state, the thread blocks until AutoResetEvent.Set is called.

呼び出すSet信号AutoResetEventを待機中のスレッドを解放します。Calling Set signals AutoResetEvent to release a waiting thread. AutoResetEvent 単一の待機スレッドが解放され、自動的に非シグナル状態に戻るまでシグナル状態に残ります。AutoResetEvent remains signaled until a single waiting thread is released, and then automatically returns to the non-signaled state. 無期限に待機しているスレッドがない場合に、状態ままシグナルです。If no threads are waiting, the state remains signaled indefinitely.

スレッドを呼び出す場合WaitOne中に、AutoResetEventはシグナルの状態でスレッドをブロックしません。If a thread calls WaitOne while the AutoResetEvent is in the signaled state, the thread does not block. AutoResetEventすぐにスレッドを解放し、非シグナル状態に戻ります。The AutoResetEvent releases the thread immediately and returns to the non-signaled state.

重要

保証はありませんが、呼び出すたび、Setメソッドは、スレッドを解放します。There is no guarantee that every call to the Set method will release a thread. 2 つの呼び出しが近すぎる、スレッドが解放される前に 2 番目の呼び出しが行われるように、1 つのスレッドが解放されます。If two calls are too close together, so that the second call occurs before a thread has been released, only one thread is released. 2 番目の呼び出しが発生しなかったかのようになります。It's as if the second call did not happen. また場合、Set待機しているスレッドがないときに呼び出されると、AutoResetEventが既にシグナル、呼び出しが影響を与えません。Also, if Set is called when there are no threads waiting and the AutoResetEvent is already signaled, the call has no effect.

初期状態を制御することができます、AutoResetEventコンス トラクターにブール値を渡すことによって:true初期状態をシグナルの場合とfalseそれ以外の場合。You can control the initial state of an AutoResetEvent by passing a Boolean value to the constructor: true if the initial state is signaled and false otherwise.

AutoResetEvent 使用することができますも、 static WaitAllWaitAnyメソッド。AutoResetEvent can also be used with the static WaitAll and WaitAny methods.

.NET Framework version 2.0 以降AutoResetEventから、新しい派生EventWaitHandleクラス。Beginning with the .NET Framework version 2.0, AutoResetEvent derives from the new EventWaitHandle class. AutoResetEventは機能的に等価、EventWaitHandleで作成されたEventResetMode.AutoResetします。An AutoResetEvent is functionally equivalent to an EventWaitHandle created with EventResetMode.AutoReset.

注意

異なり、AutoResetEventクラス、EventWaitHandleクラスが名前付きシステム同期イベントへのアクセスを提供します。Unlike the AutoResetEvent class, the EventWaitHandle class provides access to named system synchronization events.

コンストラクター

AutoResetEvent(Boolean) AutoResetEvent(Boolean) AutoResetEvent(Boolean) AutoResetEvent(Boolean)

初期状態をシグナル状態に設定するかどうかを示す Boolean 型の値を使用して、AutoResetEvent クラスの新しいインスタンスを初期化します。Initializes a new instance of the AutoResetEvent class with a Boolean value indicating whether to set the initial state to signaled.

メソッド

Close() Close() Close() Close()

現在の WaitHandle によって保持されているすべてのリソースを解放します。Releases all resources held by the current WaitHandle.

(Inherited from WaitHandle)
CreateObjRef(Type) CreateObjRef(Type) CreateObjRef(Type) CreateObjRef(Type)

リモート オブジェクトとの通信に使用するプロキシの生成に必要な情報をすべて格納しているオブジェクトを作成します。Creates an object that contains all the relevant information required to generate a proxy used to communicate with a remote object.

(Inherited from MarshalByRefObject)
Dispose() Dispose() Dispose() Dispose()

WaitHandle クラスの現在のインスタンスによって使用されているすべてのリソースを解放します。Releases all resources used by the current instance of the WaitHandle class.

(Inherited from WaitHandle)
Dispose(Boolean) Dispose(Boolean) Dispose(Boolean) Dispose(Boolean)

派生クラスでオーバーライドされると、WaitHandle によって使用されているアンマネージド リソースを解放し、オプションでマネージド リソースも解放します。When overridden in a derived class, releases the unmanaged resources used by the WaitHandle, and optionally releases the managed resources.

(Inherited from WaitHandle)
Equals(Object) Equals(Object) Equals(Object) Equals(Object)

指定したオブジェクトが、現在のオブジェクトと等しいかどうかを判断します。Determines whether the specified object is equal to the current object.

(Inherited from Object)
GetHashCode() GetHashCode() GetHashCode() GetHashCode()

既定のハッシュ関数として機能します。Serves as the default hash function.

(Inherited from Object)
GetLifetimeService() GetLifetimeService() GetLifetimeService() GetLifetimeService()

対象のインスタンスの有効期間ポリシーを制御する、現在の有効期間サービス オブジェクトを取得します。Retrieves the current lifetime service object that controls the lifetime policy for this instance.

(Inherited from MarshalByRefObject)
GetType() GetType() GetType() GetType()

現在のインスタンスの Type を取得します。Gets the Type of the current instance.

(Inherited from Object)
InitializeLifetimeService() InitializeLifetimeService() InitializeLifetimeService() InitializeLifetimeService()

このインスタンスの有効期間ポリシーを制御する有効期間サービス オブジェクトを取得します。Obtains a lifetime service object to control the lifetime policy for this instance.

(Inherited from MarshalByRefObject)
MemberwiseClone() MemberwiseClone() MemberwiseClone() MemberwiseClone()

現在の Object の簡易コピーを作成します。Creates a shallow copy of the current Object.

(Inherited from Object)
MemberwiseClone(Boolean) MemberwiseClone(Boolean) MemberwiseClone(Boolean) MemberwiseClone(Boolean)

現在の MarshalByRefObject オブジェクトの簡易コピーを作成します。Creates a shallow copy of the current MarshalByRefObject object.

(Inherited from MarshalByRefObject)
Reset() Reset() Reset() Reset()

イベントの状態を非シグナル状態に設定し、スレッドをブロックします。Sets the state of the event to nonsignaled, which causes threads to block.

Set() Set() Set() Set()

イベントの状態をシグナル状態に設定し、待機している最大で 1 つのスレッドが進行できるようにします。Sets the state of the event to signaled, which allows at most one waiting thread to proceed.

ToString() ToString() ToString() ToString()

現在のオブジェクトを表す文字列を返します。Returns a string that represents the current object.

(Inherited from Object)
WaitOne() WaitOne() WaitOne() WaitOne()

現在の WaitHandle がシグナルを受け取るまで、現在のスレッドをブロックします。Blocks the current thread until the current WaitHandle receives a signal.

(Inherited from WaitHandle)
WaitOne(Int32) WaitOne(Int32) WaitOne(Int32) WaitOne(Int32)

32 ビット符号付き整数を使用して時間間隔をミリ秒単位で指定し、現在の WaitHandle がシグナルを受信するまで、現在のスレッドをブロックします。Blocks the current thread until the current WaitHandle receives a signal, using a 32-bit signed integer to specify the time interval in milliseconds.

(Inherited from WaitHandle)
WaitOne(Int32, Boolean) WaitOne(Int32, Boolean) WaitOne(Int32, Boolean) WaitOne(Int32, Boolean)

現在の WaitHandle がシグナルを受信するまで現在のスレッドをブロックします。時間間隔を指定するために 32 ビット符号付き整数を使用し、待機の前でも同期ドメインを終了するかどうかを指定します。Blocks the current thread until the current WaitHandle receives a signal, using a 32-bit signed integer to specify the time interval and specifying whether to exit the synchronization domain before the wait.

(Inherited from WaitHandle)
WaitOne(TimeSpan) WaitOne(TimeSpan) WaitOne(TimeSpan) WaitOne(TimeSpan)

TimeSpan を使用して時間間隔を指定し、現在のインスタンスがシグナルを受信するまで現在のスレッドをブロックします。Blocks the current thread until the current instance receives a signal, using a TimeSpan to specify the time interval.

(Inherited from WaitHandle)
WaitOne(TimeSpan, Boolean) WaitOne(TimeSpan, Boolean) WaitOne(TimeSpan, Boolean) WaitOne(TimeSpan, Boolean)

現在のインスタンスがシグナルを受信するまで現在のスレッドをブロックします。TimeSpan を使用して時間間隔を指定し、待機の前でも同期ドメインを終了するかどうかを指定します。Blocks the current thread until the current instance receives a signal, using a TimeSpan to specify the time interval and specifying whether to exit the synchronization domain before the wait.

(Inherited from WaitHandle)

プロパティ

Handle Handle Handle Handle

ネイティブ オペレーティング システム ハンドルを取得または設定します。Gets or sets the native operating system handle.

(Inherited from WaitHandle)
SafeWaitHandle SafeWaitHandle SafeWaitHandle SafeWaitHandle

ネイティブ オペレーティング システム ハンドルを取得または設定します。Gets or sets the native operating system handle.

(Inherited from WaitHandle)

明示的なインターフェイスの実装

IDisposable.Dispose() IDisposable.Dispose() IDisposable.Dispose() IDisposable.Dispose()

WaitHandle によって使用されているすべてのリソースを解放します。Releases all resources used by the WaitHandle.

(Inherited from WaitHandle)

フィールド

WaitTimeout WaitTimeout WaitTimeout WaitTimeout

待機ハンドルがシグナル状態になる前に WaitAny(WaitHandle[], Int32, Boolean) 操作がタイムアウトになったことを示します。Indicates that a WaitAny(WaitHandle[], Int32, Boolean) operation timed out before any of the wait handles were signaled. このフィールドは定数です。This field is constant.

(Inherited from WaitHandle)

拡張メソッド

GetSafeWaitHandle(WaitHandle) GetSafeWaitHandle(WaitHandle) GetSafeWaitHandle(WaitHandle) GetSafeWaitHandle(WaitHandle)

ネイティブ オペレーティング システムの待機ハンドルのためのセーフ ハンドルを取得します。Gets the safe handle for a native operating system wait handle.

SetSafeWaitHandle(WaitHandle, SafeWaitHandle) SetSafeWaitHandle(WaitHandle, SafeWaitHandle) SetSafeWaitHandle(WaitHandle, SafeWaitHandle) SetSafeWaitHandle(WaitHandle, SafeWaitHandle)

ネイティブ オペレーティング システムの待機ハンドルのためのセーフ ハンドルを設定します。Sets a safe handle for a native operating system wait handle.

適用対象

スレッド セーフ

このクラスは、スレッド セーフです。This class is thread safe.

こちらもご覧ください