Vit*_*lyB 15 .net c# pinvoke synchronization mutex
请考虑以下场景:我正在运行我的应用程序,在执行期间,必须运行另一个进程,并且只有在第二个进程完成内部特定初始化之后,我的第一个进程才能继续.例如:
...
// Process1 code does various initializations here
Process.Start("Process2.exe");
// Wait until Process2 finishes its initialization and only then continue (Process2 doesn't exit)
...
Run Code Online (Sandbox Code Playgroud)
我看到几个选项:
我很高兴听到你对这个案子的意见.
谢谢!
Nic*_*ler 16
我只是想编辑这个答案,但这似乎不正确.所以我会张贴自己的......
根据Threads for C#页面,它有很多同步教程,AutoResetEvent不能用于进程间同步.
EventWaitHandle可以用于进程间同步.在上面的页面中,访问Creating a Cross-Process EventWaitHandle部分.
你设置它的方式是直截了当的:
EventWaitHandle在开始流程2之前创建流程1.EventWaitHandle.WaitOne阻止当前线程.EventWaitHandle进程2并调用EventWaitHandle.Set以释放等待的线程.过程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
}
Run Code Online (Sandbox Code Playgroud)
过程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.
Run Code Online (Sandbox Code Playgroud)
现在,关于EventResetMode.无论您选择EventResetMode.AutoReset还是EventResetMode.ManualReset取决于您的申请.
就我而言,我需要手动重置,因为我有许多进程连接到同一进程.因此,一旦初始化完成相同的过程,所有其他过程应该能够工作.因此,手柄应保持信号状态(不复位).
对于您来说,如果您必须在每次进程1启动进程2时执行初始化,则自动重置可能会有所帮助.
InterprocessProtocol.EventHandleName只是一个包含在DLL中的常量,它包括进程1和进程2引用.您不需要这样做,但它可以保护您不会错误输入名称并导致死锁.