Export (0) Print
Expand All

AutoResetEvent Constructor

Initializes a new instance of the AutoResetEvent class with a Boolean value indicating whether to set the initial state to signaled.

Namespace: System.Threading
Assembly: mscorlib (in mscorlib.dll)

public AutoResetEvent (
	bool initialState
)
public AutoResetEvent (
	boolean initialState
)
public function AutoResetEvent (
	initialState : boolean
)

Parameters

initialState

true to set the initial state to signaled; false to set the initial state to non-signaled.

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);
         }
      }
	}
}

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 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0

Community Additions

ADD
Show:
© 2014 Microsoft