AutoResetEvent(Boolean) Konstruktor

Definisi

Menginisialisasi instans AutoResetEvent baru kelas dengan nilai Boolean yang menunjukkan apakah akan mengatur status awal ke sinyal.

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

Parameter

initialState
Boolean

true untuk mengatur status awal ke sinyal; false untuk mengatur status awal ke tidak diberi sinyal.

Contoh

Contoh berikut menggunakan AutoResetEvent untuk menyinkronkan aktivitas dua utas. Utas pertama, yang merupakan utas Mainaplikasi, menjalankan . Ini menulis nilai ke sumber daya yang dilindungi, yang merupakan bidang static (Shared di Visual Basic) bernama number. Utas kedua menjalankan metode statis ThreadProc , yang membaca nilai yang ditulis oleh Main.

Metode ThreadProc menunggu AutoResetEvent. Ketika Main memanggil Set metode pada AutoResetEvent, ThreadProc metode membaca satu nilai. Segera AutoResetEvent direset, sehingga ThreadProc metode menunggu lagi.

Logika program menjamin bahwa ThreadProc metode tidak akan pernah membaca nilai yang sama dua kali. Ini tidak menjamin bahwa metode akan membaca setiap nilai yang ThreadProc ditulis oleh Main. Jaminan itu akan membutuhkan kunci kedua AutoResetEvent .

Setelah setiap operasi penulisan, Main menghasilkan dengan memanggil Thread.Sleep metode , untuk memberikan utas kedua kesempatan untuk dijalankan. Jika tidak, pada komputer Main prosesor tunggal akan menulis banyak nilai di antara dua operasi baca.

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

Berlaku untuk

Lihat juga