Bagikan melalui


AutoResetEvent Kelas

Definisi

Mewakili peristiwa sinkronisasi utas yang, ketika disinyalir, merilis satu utas tunggu tunggal lalu mengatur ulang secara otomatis. Kelas ini tidak dapat diwariskan.

public ref class AutoResetEvent sealed : System::Threading::EventWaitHandle
public ref class AutoResetEvent sealed : System::Threading::WaitHandle
public sealed class AutoResetEvent : System.Threading.EventWaitHandle
public sealed class AutoResetEvent : System.Threading.WaitHandle
[System.Runtime.InteropServices.ComVisible(true)]
public sealed class AutoResetEvent : System.Threading.EventWaitHandle
type AutoResetEvent = class
    inherit EventWaitHandle
type AutoResetEvent = class
    inherit WaitHandle
[<System.Runtime.InteropServices.ComVisible(true)>]
type AutoResetEvent = class
    inherit EventWaitHandle
Public NotInheritable Class AutoResetEvent
Inherits EventWaitHandle
Public NotInheritable Class AutoResetEvent
Inherits WaitHandle
Warisan
Warisan
Warisan
Atribut

Contoh

Contoh berikut menunjukkan cara menggunakan AutoResetEvent untuk merilis satu utas pada satu waktu, dengan memanggil metode Set (pada kelas dasar) setiap kali pengguna menekan tombol Enter. Contoh memulai tiga utas, yang menunggu pada AutoResetEvent yang dibuat dalam status sinyal. Utas pertama segera dirilis, karena AutoResetEvent sudah dalam keadaan disinyalir. Ini mengatur ulang AutoResetEvent ke status tidak diberi sinyal, sehingga blok utas berikutnya. Utas yang diblokir tidak dirilis sampai pengguna merilisnya satu per satu dengan menekan tombol Enter.

Setelah utas dirilis dari AutoResetEventpertama , alur menunggu di AutoResetEvent lain yang dibuat dalam status tidak diberi sinyal. Ketiga blok utas, sehingga metode Set harus dipanggil tiga kali untuk merilis semuanya.

using namespace System;
using namespace System::Threading;

ref class Example
{
private:
    static AutoResetEvent^ event_1 = gcnew AutoResetEvent(true);
    static AutoResetEvent^ event_2 = gcnew AutoResetEvent(false);

    static void ThreadProc()
    {
        String^ name = Thread::CurrentThread->Name;

        Console::WriteLine("{0} waits on AutoResetEvent #1.", name);
        event_1->WaitOne();
        Console::WriteLine("{0} is released from AutoResetEvent #1.", name);

        Console::WriteLine("{0} waits on AutoResetEvent #2.", name);
        event_2->WaitOne();
        Console::WriteLine("{0} is released from AutoResetEvent #2.", name);

        Console::WriteLine("{0} ends.", name);
    }

public:
    static void Demo()
    {
        Console::WriteLine("Press Enter to create three threads and start them.\r\n" +
                           "The threads wait on AutoResetEvent #1, which was created\r\n" +
                           "in the signaled state, so the first thread is released.\r\n" +
                           "This puts AutoResetEvent #1 into the unsignaled state.");
        Console::ReadLine();
            
        for (int i = 1; i < 4; i++)
        {
            Thread^ t = gcnew Thread(gcnew ThreadStart(&ThreadProc));
            t->Name = "Thread_" + i;
            t->Start();
        }
        Thread::Sleep(250);

        for (int i = 0; i < 2; i++)
        {
            Console::WriteLine("Press Enter to release another thread.");
            Console::ReadLine();
            event_1->Set();
            Thread::Sleep(250);
        }

        Console::WriteLine("\r\nAll threads are now waiting on AutoResetEvent #2.");
        for (int i = 0; i < 3; i++)
        {
            Console::WriteLine("Press Enter to release a thread.");
            Console::ReadLine();
            event_2->Set();
            Thread::Sleep(250);
        }

        // Visual Studio: Uncomment the following line.
        //Console::Readline();
    }
};

void main()
{
    Example::Demo();
}

/* This example produces output similar to the following:

Press Enter to create three threads and start them.
The threads wait on AutoResetEvent #1, which was created
in the signaled state, so the first thread is released.
This puts AutoResetEvent #1 into the unsignaled state.

Thread_1 waits on AutoResetEvent #1.
Thread_1 is released from AutoResetEvent #1.
Thread_1 waits on AutoResetEvent #2.
Thread_3 waits on AutoResetEvent #1.
Thread_2 waits on AutoResetEvent #1.
Press Enter to release another thread.

Thread_3 is released from AutoResetEvent #1.
Thread_3 waits on AutoResetEvent #2.
Press Enter to release another thread.

Thread_2 is released from AutoResetEvent #1.
Thread_2 waits on AutoResetEvent #2.

All threads are now waiting on AutoResetEvent #2.
Press Enter to release a thread.

Thread_2 is released from AutoResetEvent #2.
Thread_2 ends.
Press Enter to release a thread.

Thread_1 is released from AutoResetEvent #2.
Thread_1 ends.
Press Enter to release a thread.

Thread_3 is released from AutoResetEvent #2.
Thread_3 ends.
 */
using System;
using System.Threading;

// Visual Studio: Replace the default class in a Console project with 
//                the following class.
class Example
{
    private static AutoResetEvent event_1 = new AutoResetEvent(true);
    private static AutoResetEvent event_2 = new AutoResetEvent(false);

    static void Main()
    {
        Console.WriteLine("Press Enter to create three threads and start them.\r\n" +
                          "The threads wait on AutoResetEvent #1, which was created\r\n" +
                          "in the signaled state, so the first thread is released.\r\n" +
                          "This puts AutoResetEvent #1 into the unsignaled state.");
        Console.ReadLine();
            
        for (int i = 1; i < 4; i++)
        {
            Thread t = new Thread(ThreadProc);
            t.Name = "Thread_" + i;
            t.Start();
        }
        Thread.Sleep(250);

        for (int i = 0; i < 2; i++)
        {
            Console.WriteLine("Press Enter to release another thread.");
            Console.ReadLine();
            event_1.Set();
            Thread.Sleep(250);
        }

        Console.WriteLine("\r\nAll threads are now waiting on AutoResetEvent #2.");
        for (int i = 0; i < 3; i++)
        {
            Console.WriteLine("Press Enter to release a thread.");
            Console.ReadLine();
            event_2.Set();
            Thread.Sleep(250);
        }

        // Visual Studio: Uncomment the following line.
        //Console.Readline();
    }

    static void ThreadProc()
    {
        string name = Thread.CurrentThread.Name;

        Console.WriteLine("{0} waits on AutoResetEvent #1.", name);
        event_1.WaitOne();
        Console.WriteLine("{0} is released from AutoResetEvent #1.", name);

        Console.WriteLine("{0} waits on AutoResetEvent #2.", name);
        event_2.WaitOne();
        Console.WriteLine("{0} is released from AutoResetEvent #2.", name);

        Console.WriteLine("{0} ends.", name);
    }
}

/* This example produces output similar to the following:

Press Enter to create three threads and start them.
The threads wait on AutoResetEvent #1, which was created
in the signaled state, so the first thread is released.
This puts AutoResetEvent #1 into the unsignaled state.

Thread_1 waits on AutoResetEvent #1.
Thread_1 is released from AutoResetEvent #1.
Thread_1 waits on AutoResetEvent #2.
Thread_3 waits on AutoResetEvent #1.
Thread_2 waits on AutoResetEvent #1.
Press Enter to release another thread.

Thread_3 is released from AutoResetEvent #1.
Thread_3 waits on AutoResetEvent #2.
Press Enter to release another thread.

Thread_2 is released from AutoResetEvent #1.
Thread_2 waits on AutoResetEvent #2.

All threads are now waiting on AutoResetEvent #2.
Press Enter to release a thread.

Thread_2 is released from AutoResetEvent #2.
Thread_2 ends.
Press Enter to release a thread.

Thread_1 is released from AutoResetEvent #2.
Thread_1 ends.
Press Enter to release a thread.

Thread_3 is released from AutoResetEvent #2.
Thread_3 ends.
 */
Imports System.Threading

' Visual Studio: Replace the default class in a Console project with 
'                the following class.
Class Example

    Private Shared event_1 As New AutoResetEvent(True)
    Private Shared event_2 As New AutoResetEvent(False)

    <MTAThread()> _
    Shared Sub Main()
    
        Console.WriteLine("Press Enter to create three threads and start them." & vbCrLf & _
                          "The threads wait on AutoResetEvent #1, which was created" & vbCrLf & _
                          "in the signaled state, so the first thread is released." & vbCrLf & _
                          "This puts AutoResetEvent #1 into the unsignaled state.")
        Console.ReadLine()
            
        For i As Integer = 1 To 3
            Dim t As New Thread(AddressOf ThreadProc)
            t.Name = "Thread_" & i
            t.Start()
        Next
        Thread.Sleep(250)

        For i As Integer = 1 To 2
            Console.WriteLine("Press Enter to release another thread.")
            Console.ReadLine()

            event_1.Set()
            Thread.Sleep(250)
        Next

        Console.WriteLine(vbCrLf & "All threads are now waiting on AutoResetEvent #2.")
        For i As Integer = 1 To 3
            Console.WriteLine("Press Enter to release a thread.")
            Console.ReadLine()

            event_2.Set()
            Thread.Sleep(250)
        Next

        ' Visual Studio: Uncomment the following line.
        'Console.Readline()
    End Sub

    Shared Sub ThreadProc()
    
        Dim name As String = Thread.CurrentThread.Name

        Console.WriteLine("{0} waits on AutoResetEvent #1.", name)
        event_1.WaitOne()
        Console.WriteLine("{0} is released from AutoResetEvent #1.", name)

        Console.WriteLine("{0} waits on AutoResetEvent #2.", name)
        event_2.WaitOne()
        Console.WriteLine("{0} is released from AutoResetEvent #2.", name)

        Console.WriteLine("{0} ends.", name)
    End Sub
End Class

' This example produces output similar to the following:
'
'Press Enter to create three threads and start them.
'The threads wait on AutoResetEvent #1, which was created
'in the signaled state, so the first thread is released.
'This puts AutoResetEvent #1 into the unsignaled state.
'
'Thread_1 waits on AutoResetEvent #1.
'Thread_1 is released from AutoResetEvent #1.
'Thread_1 waits on AutoResetEvent #2.
'Thread_3 waits on AutoResetEvent #1.
'Thread_2 waits on AutoResetEvent #1.
'Press Enter to release another thread.
'
'Thread_3 is released from AutoResetEvent #1.
'Thread_3 waits on AutoResetEvent #2.
'Press Enter to release another thread.
'
'Thread_2 is released from AutoResetEvent #1.
'Thread_2 waits on AutoResetEvent #2.
'
'All threads are now waiting on AutoResetEvent #2.
'Press Enter to release a thread.
'
'Thread_2 is released from AutoResetEvent #2.
'Thread_2 ends.
'Press Enter to release a thread.
'
'Thread_1 is released from AutoResetEvent #2.
'Thread_1 ends.
'Press Enter to release a thread.
'
'Thread_3 is released from AutoResetEvent #2.
'Thread_3 ends.

Keterangan

Anda menggunakan AutoResetEvent, ManualResetEvent, dan EventWaitHandle untuk interaksi utas (atau sinyal utas). Untuk informasi selengkapnya, lihat interaksi Utas .

Utas menunggu sinyal dengan memanggil AutoResetEvent.WaitOne. Jika AutoResetEvent dalam status tidak diberi sinyal, utas akan memblokir hingga AutoResetEvent.Set dipanggil. Memanggil sinyal SetAutoResetEvent untuk merilis utas tunggu. AutoResetEvent tetap diberi sinyal sampai Reset dipanggil atau satu utas tunggu dirilis, pada saat itu secara otomatis kembali ke status tidak diberi sinyal.

Jika tidak ada utas yang menunggu ketika AutoResetEvent masuk ke status sinyal, status tetap diberi sinyal sampai utas mengamati sinyal (dengan memanggil WaitOne). Utas tersebut tidak memblokir: AutoResetEvent segera merilis utas dan kembali ke status tidak diberi sinyal.

Penting

Tidak ada jaminan bahwa setiap panggilan ke metode Set akan merilis utas. Jika dua panggilan terlalu berdekatan, sehingga panggilan kedua terjadi sebelum utas dirilis, hanya satu utas yang dirilis. Seolah-olah panggilan kedua tidak terjadi. Selain itu, jika Set dipanggil ketika tidak ada utas yang menunggu dan AutoResetEvent sudah diberi sinyal, panggilan tidak berpengaruh.

Anda dapat mengontrol status awal AutoResetEvent dengan meneruskan nilai Boolean ke konstruktor: true jika status awal diberi sinyal dan false sebaliknya.

AutoResetEvent juga dapat digunakan dengan metode staticWaitAll dan WaitAny.

AutoResetEvent berasal dari kelas EventWaitHandle. AutoResetEvent secara fungsional setara dengan EventWaitHandle yang dibuat dengan EventResetMode.AutoReset.

Nota

Tidak seperti kelas AutoResetEvent, kelas EventWaitHandle menyediakan akses ke peristiwa sinkronisasi sistem bernama.

Penting

Jenis ini mengimplementasikan antarmuka IDisposable. Setelah selesai menggunakan jenisnya, Anda harus membuangnya baik secara langsung maupun tidak langsung. Untuk membuang jenis secara langsung, panggil metode Dispose dalam blok try/catch. Untuk membuangnya secara tidak langsung, gunakan konstruksi bahasa seperti using (dalam C#) atau Using (di Visual Basic). Untuk informasi selengkapnya, lihat bagian "Menggunakan Objek yang Menerapkan IDisposable" di halaman antarmuka IDisposable.

Konstruktor

AutoResetEvent(Boolean)

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

Bidang

WaitTimeout

Menunjukkan bahwa operasi WaitAny(WaitHandle[], Int32, Boolean) kehabisan waktu sebelum salah satu handel tunggu diberi sinyal. Bidang ini konstan.

(Diperoleh dari WaitHandle)

Properti

Handle
Kedaluwarsa.
Kedaluwarsa.

Mendapatkan atau mengatur handel sistem operasi asli.

(Diperoleh dari WaitHandle)
SafeWaitHandle

Mendapatkan atau mengatur handel sistem operasi asli.

(Diperoleh dari WaitHandle)

Metode

Close()

Merilis semua sumber daya yang disimpan oleh WaitHandlesaat ini.

(Diperoleh dari WaitHandle)
CreateObjRef(Type)

Membuat objek yang berisi semua informasi relevan yang diperlukan untuk menghasilkan proksi yang digunakan untuk berkomunikasi dengan objek jarak jauh.

(Diperoleh dari MarshalByRefObject)
Dispose()

Merilis semua sumber daya yang digunakan oleh instans kelas WaitHandle saat ini.

(Diperoleh dari WaitHandle)
Dispose(Boolean)

Saat ditimpa di kelas turunan, merilis sumber daya yang tidak dikelola yang digunakan oleh WaitHandle, dan secara opsional merilis sumber daya terkelola.

(Diperoleh dari WaitHandle)
Equals(Object)

Menentukan apakah objek yang ditentukan sama dengan objek saat ini.

(Diperoleh dari Object)
GetAccessControl()

Mendapatkan objek EventWaitHandleSecurity yang mewakili keamanan kontrol akses untuk peristiwa sistem bernama yang diwakili oleh objek EventWaitHandle saat ini.

(Diperoleh dari EventWaitHandle)
GetHashCode()

Berfungsi sebagai fungsi hash default.

(Diperoleh dari Object)
GetLifetimeService()
Kedaluwarsa.

Mengambil objek layanan seumur hidup saat ini yang mengontrol kebijakan seumur hidup untuk instans ini.

(Diperoleh dari MarshalByRefObject)
GetType()

Mendapatkan Type instans saat ini.

(Diperoleh dari Object)
InitializeLifetimeService()
Kedaluwarsa.

Mendapatkan objek layanan seumur hidup untuk mengontrol kebijakan seumur hidup untuk instans ini.

(Diperoleh dari MarshalByRefObject)
MemberwiseClone()

Membuat salinan dangkal dari Objectsaat ini.

(Diperoleh dari Object)
MemberwiseClone(Boolean)

Membuat salinan dangkal objek MarshalByRefObject saat ini.

(Diperoleh dari MarshalByRefObject)
Reset()

Mengatur status peristiwa ke nonsignaled, yang menyebabkan utas diblokir.

Reset()

Mengatur status peristiwa ke nonsignaled, menyebabkan utas diblokir.

(Diperoleh dari EventWaitHandle)
Set()

Mengatur status peristiwa ke sinyal, yang memungkinkan paling banyak satu alur tunggu untuk melanjutkan.

Set()

Mengatur status peristiwa menjadi sinyal, memungkinkan satu atau beberapa alur tunggu untuk melanjutkan.

(Diperoleh dari EventWaitHandle)
SetAccessControl(EventWaitHandleSecurity)

Mengatur keamanan kontrol akses untuk peristiwa sistem bernama.

(Diperoleh dari EventWaitHandle)
ToString()

Mengembalikan string yang mewakili objek saat ini.

(Diperoleh dari Object)
WaitOne()

Memblokir utas saat ini hingga WaitHandle saat ini menerima sinyal.

(Diperoleh dari WaitHandle)
WaitOne(Int32)

Memblokir utas saat ini hingga WaitHandle saat ini menerima sinyal, menggunakan bilangan bulat bertanda tangan 32-bit untuk menentukan interval waktu dalam milidetik.

(Diperoleh dari WaitHandle)
WaitOne(Int32, Boolean)

Memblokir utas saat ini hingga WaitHandle saat ini menerima sinyal, menggunakan bilangan bulat bertanda tangan 32-bit untuk menentukan interval waktu dan menentukan apakah akan keluar dari domain sinkronisasi sebelum menunggu.

(Diperoleh dari WaitHandle)
WaitOne(TimeSpan)

Memblokir utas saat ini hingga instans saat ini menerima sinyal, menggunakan TimeSpan untuk menentukan interval waktu.

(Diperoleh dari WaitHandle)
WaitOne(TimeSpan, Boolean)

Memblokir utas saat ini hingga instans saat ini menerima sinyal, menggunakan TimeSpan untuk menentukan interval waktu dan menentukan apakah akan keluar dari domain sinkronisasi sebelum menunggu.

(Diperoleh dari WaitHandle)

Implementasi Antarmuka Eksplisit

IDisposable.Dispose()

API ini mendukung infrastruktur produk dan tidak dimaksudkan untuk digunakan langsung dari kode Anda.

Merilis semua sumber daya yang digunakan oleh WaitHandle.

(Diperoleh dari WaitHandle)

Metode Ekstensi

GetAccessControl(EventWaitHandle)

Mengembalikan deskriptor keamanan untuk handleyang ditentukan.

SetAccessControl(EventWaitHandle, EventWaitHandleSecurity)

Mengatur deskriptor keamanan untuk handel tunggu peristiwa yang ditentukan.

GetSafeWaitHandle(WaitHandle)

Mendapatkan handel yang aman untuk handel tunggu sistem operasi asli.

SetSafeWaitHandle(WaitHandle, SafeWaitHandle)

Mengatur handel yang aman untuk handel tunggu sistem operasi asli.

Berlaku untuk

Keamanan Thread

Kelas ini aman untuk utas.

Lihat juga