AutoResetEvent(Boolean) 建構函式

定義

初始化一個新的類別實例 AutoResetEvent ,並以布林值表示是否將初始狀態設為 signaled。

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 將初始狀態設為有訊號; false 將初始狀態設為無訊號。

範例

以下範例使用 an AutoResetEvent 來同步兩個執行緒的活動。 第一個執行緒,也就是應用程式執行緒,執行 Main。 它會將值寫入受保護的資源,該資源是一個名為 static(在 Visual Basic 中為 Shared)欄位,名為 number。 第二個執行緒執行靜態 ThreadProc 方法,讀取由 Main寫入的值。

方法 ThreadProc 等待 AutoResetEvent。 當 Main 呼叫 Set 的方法時 AutoResetEventThreadProc 該方法讀取一個值。 會 AutoResetEvent 立即重置,所以 ThreadProc 方法又會等一等。

程式邏輯保證 ThreadProc 方法不會重複讀取相同值兩次。 它並不保證 ThreadProc 該方法會讀取由 寫入 Main的每一個值。 這種保證需要第二個 AutoResetEvent 鎖。

每次寫入操作後, Main 透過呼叫該 Thread.Sleep 方法,讓第二個執行緒有機會執行。 否則,單處理器電腦 Main 會在任意兩次讀取操作間寫入許多值。

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

適用於

另請參閱