I was just going to edit this answer, but it doesn't seem correct. So I'll post my own...
According to the Threads for C# page, which has a lot of synchronization tutorials, AutoResetEvent
cannot be used for interprocess synchronization.
However, a named
EventWaitHandle
can be used for interprocess synchronization.
In the above page, visit the
Creating a Cross-Process EventWaitHandle section.
The way you set this up is straight-forward:
Process 1
EventWaitHandle handle = new EventWaitHandle(
false, /* Create handle in unsignaled state */
EventResetMode.ManualReset, /* Ignored. This instance doesn't reset. */
InterprocessProtocol.EventHandleName /* String defined in a shared assembly. */
);
ProcessStartInfo startInfo = new ProcessStartInfo("Process2.exe");
using (Process proc = Process.Start(startInfo))
{
//Wait for process 2 to initialize.
handle.WaitOne();
//TODO
}
Process 2
//Do some lengthy initialization work...
EventWaitHandle handle = new EventWaitHandle(
false, /* Parameter ignored since handle already exists.*/
EventResetMode.ManualReset, /* Explained below. */
InterprocessProtocol.EventHandleName /* String defined in a shared assembly. */
);
handle.Set(); //Release the thread waiting on the handle.
Now, regarding the EventResetMode
.
Whether you choose EventResetMode.AutoReset
or EventResetMode.ManualReset
depends on your application.
In my case, I needed a manual reset because I have have many processes connecting to the same process. So, once this same process is done being initialized, all of the other processes should be able to do work. Thus, the handle should be left in a signaled state (no reset).
For you, an automatic reset might be helpful if you have to perform initialization for every time process 1 starts process 2.
Side note: The
InterprocessProtocol.EventHandleName
is just a constant wrapped up inside a DLL that both
process 1 and
process 2 reference. You do not need to do this, but it protects you from mis-typing the name and causing a deadlock.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…