AutoResetEvent(Boolean) 构造函数

定义

用一个指示是否将初始状态设置为终止的布尔值初始化 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;若要将初始状态设置为非终止,则为 false

示例

以下示例使用一个同步两个 AutoResetEvent 线程的活动。 执行第一个线程,即应用程序线程 Main。 它将值写入受保护资源,该资源是static名为numberVisual Basic) 字段中的 (Shared。 第二个线程执行静态 ThreadProc 方法,该方法读取由 Main该线程写入的值。

该方法ThreadProc等待 。AutoResetEvent Set调用该方法AutoResetEventMain,该方法ThreadProc将读取一个值。 立即 AutoResetEvent 重置,因此 ThreadProc 该方法会再次等待。

程序逻辑保证 ThreadProc 该方法永远不会读取相同的值两次。 It does not guarantee that the ThreadProc method will read every value written by Main. 这种保证需要第二个 AutoResetEvent 锁。

每次写入操作后, Main 通过调用 Thread.Sleep 方法生成,使第二个线程有机会执行。 否则,在单处理器计算机上 Main ,会在任意两个读取操作之间写入多个值。

using namespace System;
using namespace System::Threading;
ref class MyMainClass
{
public:
   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 );
      }
   }


   //Initially not signaled.
   static AutoResetEvent^ myResetEvent = gcnew AutoResetEvent( false );
   static int number;
   literal int numIterations = 100;
};

int main()
{
   
   //Create and start the reader thread.
   Thread^ myReaderThread = gcnew Thread( gcnew ThreadStart( MyMainClass::MyReadThreadProc ) );
   myReaderThread->Name = "ReaderThread";
   myReaderThread->Start();
   for ( int i = 1; i <= MyMainClass::numIterations; i++ )
   {
      Console::WriteLine( "Writer thread writing value: {0}", i );
      MyMainClass::number = i;
      
      //Signal that a value has been written.
      MyMainClass::myResetEvent->Set();
      
      //Give the Reader thread an opportunity to act.
      Thread::Sleep( 1 );

   }
   
   //Terminate the reader thread.
   myReaderThread->Abort();
}
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

适用于

另请参阅