AsyncManualResetEvent Class

    Package: @esfx/async-manualresetevent

    Asynchronously notifies one or more waiting Promises that an event has occurred.

    Declaration
    export declare class AsyncManualResetEvent 

    Examples

    The following example shows how to use AsyncManualResetEvent to release asynchronous operations. In this demonstration, we start three asynchronous operations that pause when awaiting the result of wait(cancelable) because the event is unsignaled. After pressing Enter, the event becomes signaled and all three operations can continue.

    Pressing Enter again then demonstrates that two more asynchronous operations won't pause when awaiting the result of wait(cancelable) because the event is now unsignaled.

    Pressing Enter a third time calls reset() on the event and demonstrates that a new asynchronous operation will once again pause when awaiting the result of wait(cancelable) now that the event has become signaled.

    Pressing Enter a fourth and final time calls set() to signal the event and allow the final operation to conclude.

    Note

    The following example is derived from https://docs.microsoft.com/en-us/dotnet/api/system.threading.manualresetevent?view=net-6.0#examples

    • TypeScript
    • JavaScript (CommonJS)
    import { AsyncManualResetEvent } from "@esfx/async-manualresetevent";
    import { delay } from "@esfx/async-delay";
    import * as readline from "readline";
    
    const rl = readline.createInterface({ input: process.stdin, output: process.stdout });
    const question = (text: string) => new Promise<string>(resolve => rl.question(text, resolve));
    
    const evt = new AsyncManualResetEvent();
    
    async function main() {
        console.log(`Starting three operations that will pause while waiting for the event.`);
    
        let promises: Promise<void>[] = [];
        for (let i = 0; i < 3; i++) {
            promises.push(asyncOperation(`operation #${i + 1}`));
        }
    
        await delay(500);
        await question(`When all three operations have started, press Enter to release them.`);
    
        evt.set();
    
        await delay(500);
        await Promise.all(promises);
    
        console.log(`When the event is signaled, operations will not pause.`);
        await question(`Press Enter to demonstrate.`);
    
        promises = [];
        for (let i = 3; i < 4; i++) {
            promises.push(asyncOperation(`operation #${i + 1}`));
        }
    
        await delay(500);
        await Promise.all(promises);
    
        console.log(`When the event is reset, operations will again pause.`);
        await question(`Press Enter to demonstrate.`);
    
        evt.reset();
    
        promises = [];
        promises.push(asyncOperation("operation #5"));
    
        await delay(500);
        await question(`Press Enter to signal the event and conclude the demonstration.`);
        
        evt.set();
    
        await Promise.all(promises);
    }
    
    async function asyncOperation(name: string) {
        console.log(`${name} is waiting on the event.`);
        await evt.wait();
        console.log(`${name} was released from the event.`);
    }
    
    await main();
    
    import { AsyncManualResetEvent } from "@esfx/async-manualresetevent";
    import { delay } from "@esfx/async-delay";
    import * as readline from "readline";
    
    const rl = readline.createInterface({ input: process.stdin, output: process.stdout });
    const question = text => new Promise(resolve => rl.question(text, resolve));
    
    const evt = new AsyncManualResetEvent();
    
    async function main() {
        console.log(`Starting three operations that will pause while waiting for the event.`);
    
        let promises = [];
        for (let i = 0; i < 3; i++) {
            promises.push(asyncOperation(`operation #${i + 1}`));
        }
    
        await delay(500);
        await question(`When all three operations have started, press Enter to release them.`);
    
        evt.set();
    
        await delay(500);
        await Promise.all(promises);
    
        console.log(`When the event is signaled, operations will not pause.`);
        await question(`Press Enter to demonstrate.`);
    
        promises = [];
        for (let i = 3; i < 4; i++) {
            promises.push(asyncOperation(`operation #${i + 1}`));
        }
    
        await delay(500);
        await Promise.all(promises);
    
        console.log(`When the event is reset, operations will again pause.`);
        await question(`Press Enter to demonstrate.`);
    
        evt.reset();
    
        promises = [];
        promises.push(asyncOperation("operation #5"));
    
        await delay(500);
        await question(`Press Enter to signal the event and conclude the demonstration.`);
        
        evt.set();
    
        await Promise.all(promises);
    }
    
    async function asyncOperation(name) {
        console.log(`${name} is waiting on the event.`);
        await evt.wait();
        console.log(`${name} was released from the event.`);
    }
    
    main().catch(e => {
        console.error(e);
        process.exit(-1);
    });
    

    Remarks

    The AsyncAutoResetEvent and AsyncManualResetEvent classes are used to provide signaling between concurrent asynchronous operations.

    An asynchronous operation can wait until an event is signaled by awaiting the result of wait(cancelable). This causes the operation to pause until the event becomes signaled. Calling set() will signal and release all waiting operations. The event will remain in the signaled state until reset() is called, which resets the event to the non-signaled state.

    You can use isSet to check whether the event is currently signaled to avoid calling wait(cancelable) (and avoid the await that entails).

    Constructors

    constructor(initialState)

    Initializes a new instance of the ManualResetEvent class.

    Declaration
    constructor(initialState?: boolean);
    Parameters
    initialState
    boolean

    A value indicating whether to set the initial state to signaled.

    Properties

    isSet

    Gets a value indicating whether the event is signaled.

    Declaration
    get isSet(): boolean;
    Property Value
    boolean

    Methods

    reset()

    Sets the state of the event to nonsignaled, causing asynchronous operations to pause.

    Declaration
    reset(): void;
    Returns
    void

    set()

    Sets the state of the event to signaled, resolving one or more waiting Promises.

    Declaration
    set(): void;
    Returns
    void

    wait(cancelable)

    Asynchronously waits for the event to become signaled.

    Declaration
    wait(cancelable?: Cancelable): Promise<void>;
    Parameters
    cancelable
    Cancelable

    A Cancelable used to cancel the request.

    Returns
    Promise<void>

    • Improve this Doc
    Generated by DocFX