View global events generated by a native process in a .NET process

I have created a global event and set / reset in my own C ++ process, which is created as follows:

HANDLE hGlobalEvent = CreateEvent(NULL, TRUE, FALSE, _T("Global\\MyEvent"));

Is there a way (even if it is with a library not written by MS) to register for one of these events in .NET (C #), so that standard .NET event handlers fire when a global event changes?
And I just don't want to wait for the event and the loop, as in C ++ with WaitForSingleObject ... I really would like it to be a fully asynchronous event handler.

I have to imagine an easy way to do this ... I just can't find it.

+3
source share
1 answer

ThreadPool.RegisterWaitForSingleObject can be used to execute a callback when an event is signaled. Get the WaitHandle for the named event object using the EventWaitHandle constructor , which takes the name of the string.

bool createdNew;
WaitHandle waitHandle = new EventWaitHandle(false,
    EventResetMode.ManualReset, @"Global\MyEvent", out createdNew);
// createdNew should be 'false' because event already exists
ThreadPool.RegisterWaitForSingleObject(waitHandle, MyCallback, null,
    -1, true);

void MyCallback(object state, bool timedOut) { /* ... */ }
+6
source

Source: https://habr.com/ru/post/1708501/


All Articles