ManualResetEvent Klasa

Definicja

Reprezentuje zdarzenie synchronizacji wątków, które po zasygnalizowanej sytuacji musi zostać zresetowane ręcznie. Klasa ta nie może być dziedziczona.

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

Przykłady

W poniższym przykładzie pokazano, jak ManualResetEvent działa. Przykład rozpoczyna się od ManualResetEvent wartości w stanie niepodpisanym (czyli false jest przekazywany do konstruktora). W przykładzie są tworzone trzy wątki, z których każda blokuje obiekt ManualResetEvent przez wywołanie metody WaitOne . Gdy użytkownik naciska klawisz Enter , przykład wywołuje metodę Set , która zwalnia wszystkie trzy wątki. Kontrastuje to z zachowaniem AutoResetEvent klasy, która zwalnia wątki pojedynczo, resetując je automatycznie po każdej wersji.

Naciśnięcie klawisza Enter ponownie pokazuje, że ManualResetEvent pozostaje w stanie sygnału, dopóki jego Reset metoda nie zostanie wywołana: Przykład uruchamia dwa kolejne wątki. Te wątki nie blokują się, gdy wywołają metodę WaitOne , ale zamiast tego są uruchamiane do ukończenia.

Naciśnięcie klawisza Enter ponownie powoduje wywołanie Reset metody i uruchomienie jeszcze jednego wątku, który blokuje wywołanie WaitOnemetody . Naciśnięcie klawisza Enter po raz ostatni wywołuje wywołanie Set , aby zwolnić ostatni wątek, a program kończy się.

using namespace System;
using namespace System::Threading;

ref class Example
{
private:
    // mre is used to block and release threads manually. It is
    // created in the unsignaled state.
    static ManualResetEvent^ mre = gcnew ManualResetEvent(false);

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

        Console::WriteLine(name + " starts and calls mre->WaitOne()");

        mre->WaitOne();

        Console::WriteLine(name + " ends.");
    }

public:
    static void Demo()
    {
        Console::WriteLine("\nStart 3 named threads that block on a ManualResetEvent:\n");

        for(int i = 0; i <=2 ; i++)
        {
            Thread^ t = gcnew Thread(gcnew ThreadStart(ThreadProc));
            t->Name = "Thread_" + i;
            t->Start();
        }

        Thread::Sleep(500);
        Console::WriteLine("\nWhen all three threads have started, press Enter to call Set()" +
                           "\nto release all the threads.\n");
        Console::ReadLine();

        mre->Set();

        Thread::Sleep(500);
        Console::WriteLine("\nWhen a ManualResetEvent is signaled, threads that call WaitOne()" +
                           "\ndo not block. Press Enter to show this.\n");
        Console::ReadLine();

        for(int i = 3; i <= 4; i++)
        {
            Thread^ t = gcnew Thread(gcnew ThreadStart(ThreadProc));
            t->Name = "Thread_" + i;
            t->Start();
        }

        Thread::Sleep(500);
        Console::WriteLine("\nPress Enter to call Reset(), so that threads once again block" +
                           "\nwhen they call WaitOne().\n");
        Console::ReadLine();

        mre->Reset();

        // Start a thread that waits on the ManualResetEvent.
        Thread^ t5 = gcnew Thread(gcnew ThreadStart(ThreadProc));
        t5->Name = "Thread_5";
        t5->Start();

        Thread::Sleep(500);
        Console::WriteLine("\nPress Enter to call Set() and conclude the demo.");
        Console::ReadLine();

        mre->Set();

        // If you run this example in Visual Studio, uncomment the following line:
        //Console::ReadLine();
    }
};

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

/* This example produces output similar to the following:

Start 3 named threads that block on a ManualResetEvent:

Thread_0 starts and calls mre->WaitOne()
Thread_1 starts and calls mre->WaitOne()
Thread_2 starts and calls mre->WaitOne()

When all three threads have started, press Enter to call Set()
to release all the threads.


Thread_2 ends.
Thread_1 ends.
Thread_0 ends.

When a ManualResetEvent is signaled, threads that call WaitOne()
do not block. Press Enter to show this.


Thread_3 starts and calls mre->WaitOne()
Thread_3 ends.
Thread_4 starts and calls mre->WaitOne()
Thread_4 ends.

Press Enter to call Reset(), so that threads once again block
when they call WaitOne().


Thread_5 starts and calls mre->WaitOne()

Press Enter to call Set() and conclude the demo.

Thread_5 ends.
 */
using System;
using System.Threading;

public class Example
{
    // mre is used to block and release threads manually. It is
    // created in the unsignaled state.
    private static ManualResetEvent mre = new ManualResetEvent(false);

    static void Main()
    {
        Console.WriteLine("\nStart 3 named threads that block on a ManualResetEvent:\n");

        for(int i = 0; i <= 2; i++)
        {
            Thread t = new Thread(ThreadProc);
            t.Name = "Thread_" + i;
            t.Start();
        }

        Thread.Sleep(500);
        Console.WriteLine("\nWhen all three threads have started, press Enter to call Set()" +
                          "\nto release all the threads.\n");
        Console.ReadLine();

        mre.Set();

        Thread.Sleep(500);
        Console.WriteLine("\nWhen a ManualResetEvent is signaled, threads that call WaitOne()" +
                          "\ndo not block. Press Enter to show this.\n");
        Console.ReadLine();

        for(int i = 3; i <= 4; i++)
        {
            Thread t = new Thread(ThreadProc);
            t.Name = "Thread_" + i;
            t.Start();
        }

        Thread.Sleep(500);
        Console.WriteLine("\nPress Enter to call Reset(), so that threads once again block" +
                          "\nwhen they call WaitOne().\n");
        Console.ReadLine();

        mre.Reset();

        // Start a thread that waits on the ManualResetEvent.
        Thread t5 = new Thread(ThreadProc);
        t5.Name = "Thread_5";
        t5.Start();

        Thread.Sleep(500);
        Console.WriteLine("\nPress Enter to call Set() and conclude the demo.");
        Console.ReadLine();

        mre.Set();

        // If you run this example in Visual Studio, uncomment the following line:
        //Console.ReadLine();
    }

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

        Console.WriteLine(name + " starts and calls mre.WaitOne()");

        mre.WaitOne();

        Console.WriteLine(name + " ends.");
    }
}

/* This example produces output similar to the following:

Start 3 named threads that block on a ManualResetEvent:

Thread_0 starts and calls mre.WaitOne()
Thread_1 starts and calls mre.WaitOne()
Thread_2 starts and calls mre.WaitOne()

When all three threads have started, press Enter to call Set()
to release all the threads.


Thread_2 ends.
Thread_0 ends.
Thread_1 ends.

When a ManualResetEvent is signaled, threads that call WaitOne()
do not block. Press Enter to show this.


Thread_3 starts and calls mre.WaitOne()
Thread_3 ends.
Thread_4 starts and calls mre.WaitOne()
Thread_4 ends.

Press Enter to call Reset(), so that threads once again block
when they call WaitOne().


Thread_5 starts and calls mre.WaitOne()

Press Enter to call Set() and conclude the demo.

Thread_5 ends.
 */
Imports System.Threading

Public Class Example

    ' mre is used to block and release threads manually. It is
    ' created in the unsignaled state.
    Private Shared mre As New ManualResetEvent(False)

    <MTAThreadAttribute> _
    Shared Sub Main()

        Console.WriteLine(vbLf & _
            "Start 3 named threads that block on a ManualResetEvent:" & vbLf)

        For i As Integer = 0 To 2
            Dim t As New Thread(AddressOf ThreadProc)
            t.Name = "Thread_" & i
            t.Start()
        Next i

        Thread.Sleep(500)
        Console.WriteLine(vbLf & _
            "When all three threads have started, press Enter to call Set()" & vbLf & _
            "to release all the threads." & vbLf)
        Console.ReadLine()

        mre.Set()

        Thread.Sleep(500)
        Console.WriteLine(vbLf & _
            "When a ManualResetEvent is signaled, threads that call WaitOne()" & vbLf & _
            "do not block. Press Enter to show this." & vbLf)
        Console.ReadLine()

        For i As Integer = 3 To 4
            Dim t As New Thread(AddressOf ThreadProc)
            t.Name = "Thread_" & i
            t.Start()
        Next i

        Thread.Sleep(500)
        Console.WriteLine(vbLf & _
            "Press Enter to call Reset(), so that threads once again block" & vbLf & _
            "when they call WaitOne()." & vbLf)
        Console.ReadLine()

        mre.Reset()

        ' Start a thread that waits on the ManualResetEvent.
        Dim t5 As New Thread(AddressOf ThreadProc)
        t5.Name = "Thread_5"
        t5.Start()

        Thread.Sleep(500)
        Console.WriteLine(vbLf & "Press Enter to call Set() and conclude the demo.")
        Console.ReadLine()

        mre.Set()

        ' If you run this example in Visual Studio, uncomment the following line:
        'Console.ReadLine()

    End Sub


    Private Shared Sub ThreadProc()

        Dim name As String = Thread.CurrentThread.Name

        Console.WriteLine(name & " starts and calls mre.WaitOne()")

        mre.WaitOne()

        Console.WriteLine(name & " ends.")

    End Sub

End Class

' This example produces output similar to the following:
'
'Start 3 named threads that block on a ManualResetEvent:
'
'Thread_0 starts and calls mre.WaitOne()
'Thread_1 starts and calls mre.WaitOne()
'Thread_2 starts and calls mre.WaitOne()
'
'When all three threads have started, press Enter to call Set()
'to release all the threads.
'
'
'Thread_2 ends.
'Thread_0 ends.
'Thread_1 ends.
'
'When a ManualResetEvent is signaled, threads that call WaitOne()
'do not block. Press Enter to show this.
'
'
'Thread_3 starts and calls mre.WaitOne()
'Thread_3 ends.
'Thread_4 starts and calls mre.WaitOne()
'Thread_4 ends.
'
'Press Enter to call Reset(), so that threads once again block
'when they call WaitOne().
'
'
'Thread_5 starts and calls mre.WaitOne()
'
'Press Enter to call Set() and conclude the demo.
'
'Thread_5 ends.

Uwagi

Do interakcji wątków (lub sygnalizowania wątku) jest używany element ManualResetEvent, AutoResetEventi EventWaitHandle . Aby uzyskać więcej informacji, zobacz sekcję Interakcja z wątkiem lub sygnalizacyjną w artykule Omówienie elementów pierwotnych synchronizacji .

Gdy wątek rozpoczyna działanie, które musi zostać zakończone przed kontynuowaniem innych wątków, wywołuje metodę ManualResetEvent.Reset , aby umieścić ManualResetEvent w stanie niesygnalizowany. Ten wątek można traktować jako kontrolowanie elementu ManualResetEvent. Wątki, które nazywają blok ManualResetEvent.WaitOne , oczekujące na sygnał. Po zakończeniu działania przez wątek sterujący wywołuje metodę ManualResetEvent.Set , aby zasygnalizować, że wątki oczekujące mogą kontynuować. Wszystkie oczekujące wątki są zwalniane.

Gdy zostanie zasygnalizowany, pozostanie zasygnalizowany do ManualResetEvent momentu ręcznego Reset() zresetowania przez wywołanie metody . Oznacza to, że wywołania do natychmiastowego powrotu WaitOne .

Stan początkowy elementu można kontrolować, przekazując wartość logiczną do konstruktora: true jeśli stan początkowy ManualResetEvent jest sygnalizowany i false w inny sposób.

ManualResetEvent można również używać z metodami staticWaitAll i WaitAny .

Począwszy od .NET Framework wersji 2.0, ManualResetEvent pochodzi z EventWaitHandle klasy . Element A ManualResetEvent jest funkcjonalnie równoważny z utworzonym EventWaitHandle elementem EventResetMode.ManualReset.

Uwaga

ManualResetEvent W przeciwieństwie do klasy EventWaitHandle klasa zapewnia dostęp do nazwanych zdarzeń synchronizacji systemu.

Począwszy od wersji .NET Framework 4.0, System.Threading.ManualResetEventSlim klasa jest uproszczoną alternatywą dla ManualResetEventklasy .

Konstruktory

ManualResetEvent(Boolean)

Inicjuje ManualResetEvent nowe wystąpienie klasy z wartością logiczną wskazującą, czy ustawić stan początkowy na sygnalizowany.

Pola

WaitTimeout

Wskazuje, że WaitAny(WaitHandle[], Int32, Boolean) upłynął limit czasu operacji, zanim którykolwiek z uchwytów oczekiwania został zasygnalizowany. To pole jest stałe.

(Odziedziczone po WaitHandle)

Właściwości

Handle
Przestarzałe.
Przestarzałe.

Pobiera lub ustawia natywny uchwyt systemu operacyjnego.

(Odziedziczone po WaitHandle)
SafeWaitHandle

Pobiera lub ustawia natywny uchwyt systemu operacyjnego.

(Odziedziczone po WaitHandle)

Metody

Close()

Zwalnia wszystkie zasoby przechowywane przez bieżący WaitHandleelement .

(Odziedziczone po WaitHandle)
CreateObjRef(Type)

Tworzy obiekt zawierający wszystkie istotne informacje wymagane do wygenerowania serwera proxy używanego do komunikowania się z obiektem zdalnym.

(Odziedziczone po MarshalByRefObject)
Dispose()

Zwalnia wszystkie zasoby używane przez bieżące wystąpienie klasy WaitHandle.

(Odziedziczone po WaitHandle)
Dispose(Boolean)

Po zastąpieniu w klasie pochodnej zwalnia niezarządzane zasoby używane przez WaitHandleprogram i opcjonalnie zwalnia zarządzane zasoby.

(Odziedziczone po WaitHandle)
Equals(Object)

Określa, czy dany obiekt jest taki sam, jak bieżący obiekt.

(Odziedziczone po Object)
GetAccessControl()

EventWaitHandleSecurity Pobiera obiekt reprezentujący zabezpieczenia kontroli dostępu dla nazwanego zdarzenia systemowego reprezentowanego przez bieżący EventWaitHandle obiekt.

(Odziedziczone po EventWaitHandle)
GetHashCode()

Służy jako domyślna funkcja skrótu.

(Odziedziczone po Object)
GetLifetimeService()
Przestarzałe.

Pobiera bieżący obiekt usługi okresu istnienia, który kontroluje zasady okresu istnienia dla tego wystąpienia.

(Odziedziczone po MarshalByRefObject)
GetType()

Type Pobiera bieżące wystąpienie.

(Odziedziczone po Object)
InitializeLifetimeService()
Przestarzałe.

Uzyskuje obiekt usługi okresu istnienia, aby kontrolować zasady okresu istnienia dla tego wystąpienia.

(Odziedziczone po MarshalByRefObject)
MemberwiseClone()

Tworzy płytkią kopię bieżącego Objectelementu .

(Odziedziczone po Object)
MemberwiseClone(Boolean)

Tworzy płytkią kopię bieżącego MarshalByRefObject obiektu.

(Odziedziczone po MarshalByRefObject)
Reset()

Ustawia stan zdarzenia na niepodpisany, co powoduje zablokowanie wątków.

Reset()

Ustawia stan zdarzenia na niepodpisany, powodując zablokowanie wątków.

(Odziedziczone po EventWaitHandle)
Set()

Ustawia stan zdarzenia na zasygnalizowany, co umożliwia kontynuowanie co najmniej jednego wątku oczekiwania.

Set()

Ustawia stan zdarzenia na sygnał, umożliwiając kontynuowanie co najmniej jednego wątku oczekiwania.

(Odziedziczone po EventWaitHandle)
SetAccessControl(EventWaitHandleSecurity)

Ustawia zabezpieczenia kontroli dostępu dla nazwanego zdarzenia systemowego.

(Odziedziczone po EventWaitHandle)
ToString()

Zwraca ciąg reprezentujący bieżący obiekt.

(Odziedziczone po Object)
WaitOne()

Blokuje bieżący wątek, dopóki bieżący WaitHandle nie otrzyma sygnału.

(Odziedziczone po WaitHandle)
WaitOne(Int32)

Blokuje bieżący wątek do WaitHandle momentu odebrania sygnału przy użyciu 32-bitowej liczby całkowitej podpisanej w celu określenia interwału czasu w milisekundach.

(Odziedziczone po WaitHandle)
WaitOne(Int32, Boolean)

Blokuje bieżący wątek do WaitHandle momentu odebrania sygnału przy użyciu 32-bitowej liczby całkowitej podpisanej w celu określenia interwału czasu i określenia, czy należy zamknąć domenę synchronizacji przed oczekiwaniem.

(Odziedziczone po WaitHandle)
WaitOne(TimeSpan)

Blokuje bieżący wątek, dopóki bieżące wystąpienie nie otrzyma sygnału przy użyciu elementu , TimeSpan aby określić interwał czasu.

(Odziedziczone po WaitHandle)
WaitOne(TimeSpan, Boolean)

Blokuje bieżący wątek, dopóki bieżące wystąpienie nie otrzyma sygnału, przy użyciu elementu , TimeSpan aby określić interwał czasu i określić, czy należy zamknąć domenę synchronizacji przed oczekiwaniem.

(Odziedziczone po WaitHandle)

Jawne implementacje interfejsu

IDisposable.Dispose()

Ten interfejs API obsługuje infrastrukturę produktu i nie jest przeznaczony do użycia bezpośrednio z poziomu kodu.

Zwalnia wszelkie zasoby używane przez element WaitHandle.

(Odziedziczone po WaitHandle)

Metody rozszerzania

GetAccessControl(EventWaitHandle)

Zwraca deskryptory zabezpieczeń dla określonego handleelementu .

SetAccessControl(EventWaitHandle, EventWaitHandleSecurity)

Ustawia deskryptory zabezpieczeń dla określonego uchwytu oczekiwania na zdarzenia.

GetSafeWaitHandle(WaitHandle)

Pobiera bezpieczny uchwyt dla natywnego uchwytu oczekiwania systemu operacyjnego.

SetSafeWaitHandle(WaitHandle, SafeWaitHandle)

Ustawia bezpieczny uchwyt dla natywnego uchwytu oczekiwania systemu operacyjnego.

Dotyczy

Bezpieczeństwo wątkowe

Ta klasa jest bezpieczna wątkiem.

Zobacz też