简体   繁体   中英

How do I pass parameters to a HandlerRoutine?

I wanted to pass to my Ctrl signals handler an Event Object to notify the other threads of the event and behave accordingly to it.

In order to handle a console Ctrl signal we need to define a HandlerRoutine and pass it to the SetConsoleCtrlHandler function. But from the documentation page of the HandlerRoutine it seems like the only parameter we can pass to it is the type of Ctrl signal to handle.

How can I pass other parameters to my handler?

Since your handler is essentially stateless, if you want one thread to notify all the threads, it will require a global variable. One possibility is to use a single global to mark the received event.

received_events revents;

BOOL handler (DWORD e) {
    set_received_event(&revents, e);
    wait_if_needed_for_threads_to_handle_event(&revents, e);
    return TRUE;
}

Then all the threads will have to check this global to see if something of interest happened:

extern received_events revents;

while (!done) {
    if (received_an_event(&revents)) {
        e = which_event(&revents);
        /* ... */
        mark_event_as_handled_maybe_exit_thread(&revents, e);
    }
    do_what_I_normally_do();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM