AutoResetEvent(Boolean) コンストラクター

定義

初期状態を signaled に設定するかどうかを示すブール値を使用して、 AutoResetEvent クラスの新しいインスタンスを初期化します。

public:
 AutoResetEvent(bool initialState);
public AutoResetEvent(bool initialState);
new System.Threading.AutoResetEvent : bool -> System.Threading.AutoResetEvent
Public Sub New (initialState As Boolean)

パラメーター

initialState
Boolean

true 初期状態を signaled に設定する場合。初期状態を非シグナル状態に設定する false

次の例では、 AutoResetEvent を使用して 2 つのスレッドのアクティビティを同期します。 アプリケーション スレッドである最初のスレッドは、 Main実行されます。 保護されたリソースに値を書き込みます。これは、static (Visual Basic では Shared) number という名前のフィールドです。 2 番目のスレッドは、Mainによって書き込まれた値を読み取る静的ThreadProc メソッドを実行します。

ThreadProc メソッドは、AutoResetEventを待機します。 AutoResetEvent Main Set メソッドを呼び出すと、ThreadProc メソッドは 1 つの値を読み取ります。 AutoResetEventはすぐにリセットされるため、ThreadProc メソッドはもう一度待機します。

プログラム ロジックは、 ThreadProc メソッドが同じ値を 2 回読み取ることは決してないことを保証します。 ThreadProc メソッドがMainによって書き込まれたすべての値を読み取る保証はありません。 その保証には、2 つ目の AutoResetEvent ロックが必要です。

各書き込み操作の後、MainThread.Sleep メソッドを呼び出して生成され、2 番目のスレッドが実行されます。 それ以外の場合、単一プロセッサ コンピューターでは、 Main は、任意の 2 つの読み取り操作の間に多くの値を書き込みます。

using System;
using System.Threading;

namespace AutoResetEvent_Examples
{
    class MyMainClass
    {
        //Initially not signaled.
      const int numIterations = 100;
      static AutoResetEvent myResetEvent = new AutoResetEvent(false);
      static int number;
      
      static void Main()
        {
         //Create and start the reader thread.
         Thread myReaderThread = new Thread(new ThreadStart(MyReadThreadProc));
         myReaderThread.Name = "ReaderThread";
         myReaderThread.Start();

         for(int i = 1; i <= numIterations; i++)
         {
            Console.WriteLine("Writer thread writing value: {0}", i);
            number = i;
            
            //Signal that a value has been written.
            myResetEvent.Set();
            
            //Give the Reader thread an opportunity to act.
            Thread.Sleep(1);
         }

         //Terminate the reader thread.
         myReaderThread.Abort();
      }

      static void MyReadThreadProc()
      {
         while(true)
         {
            //The value will not be read until the writer has written
            // at least once since the last read.
            myResetEvent.WaitOne();
            Console.WriteLine("{0} reading value: {1}", Thread.CurrentThread.Name, number);
         }
      }
    }
}
Imports System.Threading

Namespace AutoResetEvent_Examples
    Class MyMainClass
        'Initially not signaled.
        Private Const numIterations As Integer = 100
        Private Shared myResetEvent As New AutoResetEvent(False)
        Private Shared number As Integer

        <MTAThread> _
        Shared Sub Main()
            'Create and start the reader thread.
            Dim myReaderThread As New Thread(AddressOf MyReadThreadProc)
            myReaderThread.Name = "ReaderThread"
            myReaderThread.Start()

            Dim i As Integer
            For i = 1 To numIterations
                Console.WriteLine("Writer thread writing value: {0}", i)
                number = i

                'Signal that a value has been written.
                myResetEvent.Set()

                'Give the Reader thread an opportunity to act.
                Thread.Sleep(1)
            Next i

            'Terminate the reader thread.
            myReaderThread.Abort()
        End Sub

        Shared Sub MyReadThreadProc()
            While True
                'The value will not be read until the writer has written
                ' at least once since the last read.
                myResetEvent.WaitOne()
                Console.WriteLine("{0} reading value: {1}", Thread.CurrentThread.Name, number)
            End While
        End Sub
    End Class
End Namespace 'AutoResetEvent_Examples

適用対象

こちらもご覧ください