RegisteredWaitHandle Osztály

Definíció

A híváskor RegisterWaitForSingleObject(WaitHandle, WaitOrTimerCallback, Object, UInt32, Boolean)regisztrált leírót jelöli. Ez az osztály nem örökölhető.

public ref class RegisteredWaitHandle sealed : MarshalByRefObject
public ref class RegisteredWaitHandle sealed
[System.Runtime.Versioning.UnsupportedOSPlatform("browser")]
public sealed class RegisteredWaitHandle : MarshalByRefObject
public sealed class RegisteredWaitHandle
public sealed class RegisteredWaitHandle : MarshalByRefObject
[System.Runtime.InteropServices.ComVisible(true)]
public sealed class RegisteredWaitHandle : MarshalByRefObject
[<System.Runtime.Versioning.UnsupportedOSPlatform("browser")>]
type RegisteredWaitHandle = class
    inherit MarshalByRefObject
type RegisteredWaitHandle = class
type RegisteredWaitHandle = class
    inherit MarshalByRefObject
[<System.Runtime.InteropServices.ComVisible(true)>]
type RegisteredWaitHandle = class
    inherit MarshalByRefObject
Public NotInheritable Class RegisteredWaitHandle
Inherits MarshalByRefObject
Public NotInheritable Class RegisteredWaitHandle
Öröklődés
RegisteredWaitHandle
Öröklődés
RegisteredWaitHandle
Attribútumok

Példák

Az alábbi példa bemutatja, hogyan határozható meg a RegisteredWaitHandle visszahívási metódus meghívása, és hogyan lehet megszüntetni a feladat regisztrációjának megszüntetését, ha a visszahívás a várakozási fogópont jelzése miatt történt.

A példa azt is bemutatja, hogyan hajthat RegisterWaitForSingleObject végre egy megadott visszahívási metódust a megadott várakozási fogópont jelzésekor. Ebben a példában a visszahívási módszer, WaitProca várakozási fogópont pedig egy AutoResetEvent.

A példa meghatároz egy osztályt TaskInfo , amely a visszahívásnak átadott adatokat tárolja a végrehajtáskor. A példa létrehoz egy TaskInfo objektumot, és sztringadatokat rendel hozzá. A RegisteredWaitHandle metódus által visszaadott értéket a RegisterWaitForSingleObject rendszer az HandleTaskInfo objektum mezőjéhez rendeli hozzá, hogy a visszahívási metódus hozzáférhessen a RegisteredWaitHandle.

A visszahívási metódusnak átadni kívánt objektum megadása mellett TaskInfo a metódus hívása RegisterWaitForSingleObject meghatározza a AutoResetEvent feladat várakozási idejét, a WaitOrTimerCallback visszahívási metódust képviselő WaitProc meghatalmazottat, egy másodperces időtúllépési időközt és több visszahívást.

Amikor a fő szál a metódus meghívásával AutoResetEvent jelzi a Set jelet, a WaitOrTimerCallback meghatalmazott meghívása történik. A WaitProc metódustesztek RegisteredWaitHandle annak megállapítására, hogy történt-e időtúllépés. Ha a visszahívás azért lett meghívva, mert a várakozási leírót jelezték, a WaitProc metódus megszünteti a regisztrációt, és leállítja a RegisteredWaitHandletovábbi visszahívásokat. Időtúllépés esetén a feladat továbbra is várakozik. A WaitProc metódus úgy ér véget, hogy egy üzenetet nyomtat a konzolra.

using System;
using System.Threading;

// TaskInfo contains data that will be passed to the callback
// method.
public class TaskInfo {
    public RegisteredWaitHandle Handle = null;
    public string OtherInfo = "default";
}

public class Example {
    public static void Main(string[] args) {
        // The main thread uses AutoResetEvent to signal the
        // registered wait handle, which executes the callback
        // method.
        AutoResetEvent ev = new AutoResetEvent(false);

        TaskInfo ti = new TaskInfo();
        ti.OtherInfo = "First task";
        // The TaskInfo for the task includes the registered wait
        // handle returned by RegisterWaitForSingleObject.  This
        // allows the wait to be terminated when the object has
        // been signaled once (see WaitProc).
        ti.Handle = ThreadPool.RegisterWaitForSingleObject(
            ev,
            new WaitOrTimerCallback(WaitProc),
            ti,
            1000,
            false
        );

        // The main thread waits three seconds, to demonstrate the
        // time-outs on the queued thread, and then signals.
        Thread.Sleep(3100);
        Console.WriteLine("Main thread signals.");
        ev.Set();

        // The main thread sleeps, which should give the callback
        // method time to execute.  If you comment out this line, the
        // program usually ends before the ThreadPool thread can execute.
        Thread.Sleep(1000);
        // If you start a thread yourself, you can wait for it to end
        // by calling Thread.Join.  This option is not available with 
        // thread pool threads.
    }
   
    // The callback method executes when the registered wait times out,
    // or when the WaitHandle (in this case AutoResetEvent) is signaled.
    // WaitProc unregisters the WaitHandle the first time the event is 
    // signaled.
    public static void WaitProc(object state, bool timedOut) {
        // The state object must be cast to the correct type, because the
        // signature of the WaitOrTimerCallback delegate specifies type
        // Object.
        TaskInfo ti = (TaskInfo) state;

        string cause = "TIMED OUT";
        if (!timedOut) {
            cause = "SIGNALED";
            // If the callback method executes because the WaitHandle is
            // signaled, stop future execution of the callback method
            // by unregistering the WaitHandle.
            if (ti.Handle != null)
                ti.Handle.Unregister(null);
        } 

        Console.WriteLine("WaitProc( {0} ) executes on thread {1}; cause = {2}.",
            ti.OtherInfo, 
            Thread.CurrentThread.GetHashCode().ToString(), 
            cause
        );
    }
}
Imports System.Threading

' TaskInfo contains data that will be passed to the callback
' method.
Public Class TaskInfo
    public Handle As RegisteredWaitHandle = Nothing
    public OtherInfo As String = "default"
End Class

Public Class Example

    <MTAThread> _
    Public Shared Sub Main()
        ' The main thread uses AutoResetEvent to signal the
        ' registered wait handle, which executes the callback
        ' method.
        Dim ev As New AutoResetEvent(false)

        Dim ti As New TaskInfo()
        ti.OtherInfo = "First task"
        ' The TaskInfo for the task includes the registered wait
        ' handle returned by RegisterWaitForSingleObject.  This
        ' allows the wait to be terminated when the object has
        ' been signaled once (see WaitProc).
        ti.Handle = ThreadPool.RegisterWaitForSingleObject( _
            ev, _
            New WaitOrTimerCallback(AddressOf WaitProc), _
            ti, _
            1000, _
            false _
        )

        ' The main thread waits about three seconds, to demonstrate 
        ' the time-outs on the queued task, and then signals.
        Thread.Sleep(3100)
        Console.WriteLine("Main thread signals.")
        ev.Set()

        ' The main thread sleeps, which should give the callback
        ' method time to execute.  If you comment out this line, the
        ' program usually ends before the ThreadPool thread can execute.
        Thread.Sleep(1000)
        ' If you start a thread yourself, you can wait for it to end
        ' by calling Thread.Join.  This option is not available with 
        ' thread pool threads.
    End Sub
   
    ' The callback method executes when the registered wait times out,
    ' or when the WaitHandle (in this case AutoResetEvent) is signaled.
    ' WaitProc unregisters the WaitHandle the first time the event is 
    ' signaled.
    Public Shared Sub WaitProc(state As Object, timedOut As Boolean)
        ' The state object must be cast to the correct type, because the
        ' signature of the WaitOrTimerCallback delegate specifies type
        ' Object.
        Dim ti As TaskInfo = CType(state, TaskInfo)

        Dim cause As String = "TIMED OUT"
        If Not timedOut Then
            cause = "SIGNALED"
            ' If the callback method executes because the WaitHandle is
            ' signaled, stop future execution of the callback method
            ' by unregistering the WaitHandle.
            If Not ti.Handle Is Nothing Then
                ti.Handle.Unregister(Nothing)
            End If
        End If 

        Console.WriteLine("WaitProc( {0} ) executes on thread {1}; cause = {2}.", _
            ti.OtherInfo, _
            Thread.CurrentThread.GetHashCode().ToString(), _
            cause _
        )
    End Sub
End Class

Metódusok

Name Description
CreateObjRef(Type)

Létrehoz egy objektumot, amely tartalmazza a távoli objektumokkal való kommunikációhoz használt proxy létrehozásához szükséges összes releváns információt.

(Öröklődés forrása MarshalByRefObject)
Equals(Object)

Meghatározza, hogy a megadott objektum egyenlő-e az aktuális objektummal.

(Öröklődés forrása Object)
Finalize()

Lehetővé teszi az objektumok számára, hogy megpróbálják felszabadítani az erőforrásokat, és más tisztítási műveleteket hajtsanak végre, mielőtt a szemétgyűjtés visszanyeri azt.

GetHashCode()

Ez az alapértelmezett kivonatoló függvény.

(Öröklődés forrása Object)
GetLifetimeService()
Elavult.

Lekéri a példány élettartamszabályzatát vezérlő aktuális élettartam-szolgáltatásobjektumot.

(Öröklődés forrása MarshalByRefObject)
GetType()

Lekéri az Type aktuális példányt.

(Öröklődés forrása Object)
InitializeLifetimeService()
Elavult.

Beolvas egy élettartam-szolgáltatásobjektumot a példány élettartam-szabályzatának szabályozásához.

(Öröklődés forrása MarshalByRefObject)
MemberwiseClone()

Az aktuális Objectpéldány sekély másolatát hozza létre.

(Öröklődés forrása Object)
MemberwiseClone(Boolean)

Az aktuális MarshalByRefObject objektum sekély másolatát hozza létre.

(Öröklődés forrása MarshalByRefObject)
ToString()

Az aktuális objektumot jelképező sztringet ad vissza.

(Öröklődés forrása Object)
Unregister(WaitHandle)

Megszakítja a metódus által kiadott regisztrált várakozási RegisterWaitForSingleObject(WaitHandle, WaitOrTimerCallback, Object, UInt32, Boolean) műveletet.

A következőre érvényes:

Szálbiztonság

Ez a típus szálbiztos.

Lásd még