AutoResetEvent 构造函数
.NET Framework 类库
AutoResetEvent 构造函数

更新:2007 年 11 月

用一个指示是否将初始状态设置为终止的布尔值初始化 AutoResetEvent 类的新实例。

命名空间:  System.Threading
程序集:  mscorlib(在 mscorlib.dll 中)

Visual Basic(声明)
Public Sub New ( _
    initialState As Boolean _
)
Visual Basic (用法)
Dim initialState As Boolean

Dim instance As New AutoResetEvent(initialState)
C#
public AutoResetEvent(
    bool initialState
)
Visual C++
public:
AutoResetEvent(
    bool initialState
)
J#
public AutoResetEvent(
    boolean initialState
)
JScript
public function AutoResetEvent(
    initialState : boolean
)

参数

initialState
类型:System..::.Boolean

若要将初始状态设置为终止,则为 true;若要将初始状态设置为非终止,则为 false

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

ThreadProc 方法等待 AutoResetEvent。当 MainAutoResetEvent 中调用 Set 方法时,ThreadProc 方法将读取一个值。AutoResetEvent 立即重置,因此 ThreadProc 方法将再次等待。

程序逻辑可确保 ThreadProc 方法永远不会两次读取同一个值。它并不确保 ThreadProc 方法将读取由 Main 写入的每一个值。要确保这一点,则要求具有第二个 AutoResetEvent 锁定。

在每一次写入操作之后,需要调用 Thread..::.Sleep 方法才能生成 Main,以便第二个线程可以执行。否则,在单处理器计算机上,Main 将在任意两个读取操作之间写入许多值。

Visual Basic
Imports System
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(0)
            Next i

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

        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 'MyReadThreadProc
    End Class 'MyMainClass
End Namespace 'AutoResetEvent_Examples
C#
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(0);
         }

         //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);
         }
      }
    }
}
Visual C++
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( 0 );

   }

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

J#
package AutoResetEvent_Examples;

import System.*;
import System.Threading.*;
import System.Threading.Thread;

class MyMainClass
{
    //Initially not signaled.
    private final static int numIterations = 100;
    private static AutoResetEvent myResetEvent = new AutoResetEvent(false);
    private static int number;

    public static void main(String[] args)
    {
        //Create and start the reader thread.
        Thread myReaderThread = new Thread(new ThreadStart(MyReadThreadProc));
        myReaderThread.set_Name("ReaderThread");
        myReaderThread.Start();
        for (int i = 1; i <= numIterations; i++) {
            Console.WriteLine("Writer thread writing value: {0}",
                String.valueOf(i));
            number = i;

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

            //Give the Reader thread an opportunity to act.
            Thread.Sleep(0);
        }

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

    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.get_CurrentThread().get_Name(),String.valueOf(number));
        }
    } //MyReadThreadProc
} //MyMainClass

Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC, Xbox 360

.NET Framework 和 .NET Compact Framework 并不是对每个平台的所有版本都提供支持。有关支持的版本的列表,请参见.NET Framework 系统要求

.NET Framework

受以下版本支持:3.5、3.0、2.0、1.1、1.0

.NET Compact Framework

受以下版本支持:3.5、2.0、1.0

XNA Framework

受以下版本支持:2.0、1.0
社区内容   什么是社区内容?
添加新内容 RSS  批注
Processing
Page view tracker