Search code examples
c#concurrencysynchronizationinterprocess

Wait until all processes notified after EventWaitHandle.Set()


I do in one process:

_eventWaitHandle.Set();
_eventWaitHandle.Reset();

In another process:

_eventWaitHandle.WaitOne();
Console.WriteLine("Hello");

But never get notified (no console output). It seem that Set in asynchronous.

How can I wait until all waiters were signaled before doing Reset()?

I create wait handle (NAMED inter process wait handle):

    internal static EventWaitHandle OpenExistingOrCreateEventWaitHandle(string name)
    {
        try
        {
            return EventWaitHandle.OpenExisting(name);
        }
        catch (WaitHandleCannotBeOpenedException)
        {
            return new EventWaitHandle(false, EventResetMode.ManualReset, name);
        }
    }

UPDATE

For now I have one "solution"

_eventWaitHandle.Set();
Thread.Sleep(10);
_eventWaitHandle.Reset();

Second possible - to have many handles for each process. But this code should work in any office application add-in or standalone app. So names should be generated some how and discovered.

Third - to use WCF p2p(netPeerTcpBinding) or named pipes with UdpDiscoveryEndpoint- but these use "IP" so can have some security issues when deployed to end users?


Solution

  • I solved problem. I used memory mapped file to store list of event wait handles names. Timeout failed to work stably. Current solution works in production during 2 years.

    To have p2p like IPC desktop events I used next receipt:

    • 1 shared mutex
    • 1 unique waiter and 1 unique responder event wait handle per process (eventing participant)
    • 1 memory mapped file to store registry of participants waiting (could use real registry for this)
    • 1 memory mapped file for event data exchanged