Lire en anglais Modifier

Partager via


WaitOrTimerCallback Delegate

Definition

Represents a method to be called when a WaitHandle is signaled or times out.

public delegate void WaitOrTimerCallback(object? state, bool timedOut);
public delegate void WaitOrTimerCallback(object state, bool timedOut);
[System.Runtime.InteropServices.ComVisible(true)]
public delegate void WaitOrTimerCallback(object state, bool timedOut);

Parameters

state
Object

An object containing information to be used by the callback method each time it executes.

timedOut
Boolean

true if the WaitHandle timed out; false if it was signaled.

Attributes

Examples

The following example shows how to use the WaitOrTimerCallback delegate to represent a callback method that is executed when a wait handle is signaled.

The example also shows how to use the RegisterWaitForSingleObject method to execute a specified callback method when a specified wait handle is signaled. In this example, the callback method is WaitProc and the wait handle is an AutoResetEvent.

The example defines a TaskInfo class to hold the information that is passed to the callback when it executes. The example creates a TaskInfo object and assigns it some string data. The RegisteredWaitHandle that is returned by the RegisterWaitForSingleObject method is assigned to the Handle field of the TaskInfo object, so that the callback method has access to the RegisteredWaitHandle.

In addition to the TaskInfo object, the call to the RegisterWaitForSingleObject method specifies the AutoResetEvent the task waits on, a WaitOrTimerCallback delegate that represents the WaitProc callback method, a one-second timeout interval, and multiple callbacks.

When the main thread signals the AutoResetEvent by calling its Set method, the WaitOrTimerCallback delegate is invoked. The WaitProc method tests RegisteredWaitHandle to determine whether a timeout occurred. If the callback was invoked because the wait handle was signaled, the WaitProc method unregisters the RegisteredWaitHandle, stopping further callbacks. In the case of a timeout, the task continues waiting. The WaitProc method ends by printing a message to the console.

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
        );
    }
}

Remarks

WaitOrTimerCallback represents a callback method that you want to execute when a registered wait handle times out or is signaled. Create the delegate by passing your callback method to the WaitOrTimerCallback constructor. Your method must have the signature shown here.

Create the registered wait handle by passing the WaitOrTimerCallback delegate and a WaitHandle to ThreadPool.RegisterWaitForSingleObject. Your callback method executes each time the WaitHandle times out or is signaled.

Notes

Visual Basic users can omit the WaitOrTimerCallback constructor, and simply use the AddressOf operator when passing the callback method to RegisterWaitForSingleObject. Visual Basic automatically calls the correct delegate constructor.

If you want to pass information to your callback method, create an object that contains the necessary information and pass it to RegisterWaitForSingleObject when you create the registered wait handle. Each time your callback method executes, the state parameter contains this object.

For more information about using callback methods to synchronize thread pool threads, see The managed thread pool.

Extension Methods

GetMethodInfo(Delegate)

Gets an object that represents the method represented by the specified delegate.

Applies to

Produit Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

See also