AutoResetEvent Kelas

Definisi

Mewakili peristiwa sinkronisasi utas yang, saat diberi sinyal, 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 Set metode (pada kelas dasar) setiap kali pengguna menekan tombol Enter . Contohnya memulai tiga utas, yang menunggu pada AutoResetEvent yang dibuat dalam status sinyal. Utas pertama segera dirilis, karena AutoResetEvent sudah dalam keadaan tersinyal. Ini mengatur ulang AutoResetEvent ke status tidak disinyalkan, sehingga blok utas berikutnya. Utas yang diblokir tidak dirilis sampai pengguna merilisnya satu per satu dengan menekan tombol Enter .

Setelah utas dirilis dari yang pertama AutoResetEvent, mereka menunggu di utas lain AutoResetEvent yang dibuat dalam status tidak diberi sinyal. Ketiga blok utas, sehingga Set metode harus dipanggil tiga kali untuk melepaskan 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. AutoResetEvent Jika dalam status tidak diberi sinyal, utas akan memblokir hingga AutoResetEvent.Set dipanggil. Memanggil Set sinyal untuk merilis utas AutoResetEvent 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 bersinyali, status tetap diberi sinyal sampai utas mengamati sinyal (dengan memanggil WaitOne). Utas tersebut AutoResetEvent tidak memblokir: segera melepaskan utas dan kembali ke status tidak diberi sinyal.

Penting

Tidak ada jaminan bahwa setiap panggilan ke Set metode 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 disinyalir, panggilan tidak berpengaruh.

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

AutoResetEvent juga dapat digunakan dengan staticWaitAll metode dan WaitAny .

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

Catatan

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

Penting

Jenis ini mengimplementasikan IDisposable antarmuka. Ketika Anda telah selesai menggunakan jenis , Anda harus membuangnya baik secara langsung atau tidak langsung. Untuk membuang jenis secara langsung, panggil metodenya Dispose dalam try/catch blok. 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 Mengimplementasikan IDisposable" di IDisposable halaman antarmuka.

Konstruktor

AutoResetEvent(Boolean)

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

Bidang

WaitTimeout

Menunjukkan bahwa WaitAny(WaitHandle[], Int32, Boolean) waktu operasi habis 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 dipegang oleh saat ini WaitHandle.

(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 WaitHandle kelas saat ini.

(Diperoleh dari WaitHandle)
Dispose(Boolean)

Ketika ditimpa di kelas turunan, merilis sumber daya tidak terkelola 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()

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

(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 instans Type 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 yang saat ini Object.

(Diperoleh dari Object)
MemberwiseClone(Boolean)

Membuat salinan dangkal objek saat ini MarshalByRefObject .

(Diperoleh dari MarshalByRefObject)
Reset()

Mengatur status peristiwa ke nonsignaled, yang menyebabkan utas diblokir.

Reset()

Mengatur status peristiwa ke tidak ditandatangani, menyebabkan utas diblokir.

(Diperoleh dari EventWaitHandle)
Set()

Mengatur status peristiwa menjadi sinyal, yang memungkinkan paling banyak satu utas tunggu untuk melanjutkan.

Set()

Mengatur status peristiwa yang akan disinyalir, memungkinkan satu atau beberapa utas tunggu untuk dilanjutkan.

(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 saat ini WaitHandle menerima sinyal.

(Diperoleh dari WaitHandle)
WaitOne(Int32)

Memblokir utas saat ini hingga saat ini WaitHandle 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 saat ini WaitHandle 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 WaitHandledigunakan oleh .

(Diperoleh dari WaitHandle)

Metode Ekstensi

GetAccessControl(EventWaitHandle)

Mengembalikan deskriptor keamanan untuk yang ditentukan handle.

SetAccessControl(EventWaitHandle, EventWaitHandleSecurity)

Mengatur deskriptor keamanan untuk penanganan tunggu peristiwa yang ditentukan.

GetSafeWaitHandle(WaitHandle)

Mendapatkan handel 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