I have a system-wide manual reset event that I create by doing the following:
EventWaitHandle notifyEvent = new EventWaitHandle(false, EventResetMode.ManualReset, notifyEventName, out createdEvent);
Several processes create this event (e.g. it is shared amongst them). It is used for notifying when something gets updated.
I'd like to be able to set this event so that all of processes waiting on it are signaled and then immediately reset it so that subsequent Waits on the event are blocked.
If I do a
notifyEvent.Set();
notifyEvent.Reset();
It will sometimes notify all listening processes.
If I do a
notifyEvent.Set();
Thread.Sleep(0);
notifyEvent.Reset();
More processes get notified (I assumed this would happen since the scheduler has a chance to run).
And if I do
notifyEvent.Set();
Thread.Sleep(100);
notifyEvent.Reset();
Then everything seems to work out fine and all processes (e.g. ~8) get notified consistently. I don't like the use of a "magic number" for the Sleep call.
Is there a better way to notify all listeners of an OS event in other processes that an event has occurred so that everyone listening to it at the time of notification receive the event signal and then immediately reset the event so that anyone else that goes to listen to the event will block?
UPDATE: A Semaphore doesn't seem to be a good fit here since the number of listeners to the event can vary over time. It is not known in advance how many listeners there will be when an even needs to be notified.