Search code examples
c#.net

Confuse about the name of EventWaitHandle


Considering below code snippet for MyServer side

public void CreateEvent()
{
   var serverReadyEvent = new EventWaitHandle(false, EventResetMode.AutoReset, "aaa");
}

After search from MSDN, the 3rd parameter of the ctor means

name

Type: System.String

The name of a system-wide synchronization event.

But I find that I can call the method CreateEvent for multiple times and without any exception, is this mean that there are multiple EventWaitHandle instances exist with the same name? Is this correct behavior?

And below is the code snippet on MyClient side

public void OpenEvent()
{
     EventWaitHandle.OpenExisting("aaa");
}

If the CreateEvent was called in MyServer side twice, then in the MyClient side which event will be opened after OpenEvent was called?


Solution

  • Look at the documentation for CreateEvent().

    The documentation for the lpName parameter states:

    If lpName matches the name of an existing named event object, this function requests the EVENT_ALL_ACCESS access right. In this case, the bManualReset and bInitialState parameters are ignored because they have already been set by the creating process.

    So you can see that if the event already exists, a handle to the existing one will be returned.

    Therefore only one event exists, and the answer to your question about which one is opened is "the one and only event that was created".