Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/315.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用进程间同步对象同步2个进程-互斥或AutoResetEvent_C#_.net_Synchronization_Pinvoke_Mutex - Fatal编程技术网

C# 使用进程间同步对象同步2个进程-互斥或AutoResetEvent

C# 使用进程间同步对象同步2个进程-互斥或AutoResetEvent,c#,.net,synchronization,pinvoke,mutex,C#,.net,Synchronization,Pinvoke,Mutex,考虑以下场景:我正在运行我的应用程序,该应用程序在执行期间必须运行另一个进程,只有在第二个进程完成内部特定的初始化之后,我的第一个进程才能继续。例如: ... // Process1 code does various initializations here Process.Start("Process2.exe"); // Wait until Process2 finishes its initialization and only then continue (Process2 does

考虑以下场景:我正在运行我的应用程序,该应用程序在执行期间必须运行另一个进程,只有在第二个进程完成内部特定的初始化之后,我的第一个进程才能继续。例如:

...
// Process1 code does various initializations here
Process.Start("Process2.exe");
// Wait until Process2 finishes its initialization and only then continue (Process2 doesn't exit)
...
我看到几个选择:

  • 互斥体-在考虑进程间通信时,互斥体会自动浮现在脑海中,但是,我看不出有什么方法可以让Process1等待他自己生成的互斥体。我可以让Process2创建一个互斥体,并等待Process1创建互斥体(使用polling和mutex.OpenExisting函数)
  • AutoResetEvent-这些将非常适合此任务,但是在.NET下,它们似乎不能用于进程间通信
  • CreateEvent-我可以使用P/Invoke和Win32 CreateEvent函数。理论上,它可以提供我所需要的一切。但是,如果可能的话,我宁愿不使用本机函数
  • 使用外部文件-最简单的方法就是使用一些操作系统外部对象(文件、注册表等)。然而,这似乎有点骇人听闻
  • 我很高兴听到你对这个案子的意见


    谢谢 阅读创建跨进程EventWaitHandle部分…

    我刚要编辑,但它似乎不正确。所以我会发布我自己的

    根据有很多同步教程的页面,不能用于进程间同步。
    但是,可以使用命名的进程间同步。 在上面的页面中,访问该部分

    您的设置方式很简单:

    • 在启动流程2之前,在流程1中创建一个
    • 启动进程2后,调用以阻止当前线程
    • 最后,在进程2中创建一个线程并调用以释放等待的线程
    过程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
    }
    
    过程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.
    
    现在,关于这个问题。 选择
    EventResetMode.AutoReset
    还是
    EventResetMode.ManualReset
    取决于您的应用程序

    在我的例子中,我需要手动重置,因为我有许多进程连接到同一进程。因此,一旦完成了相同的进程的初始化,所有其他进程都应该能够完成工作。因此,手柄应保持在信号状态(无复位)

    对于您来说,如果每次流程1启动流程2时都必须执行初始化,则自动重置可能会有所帮助



    旁注:进程间协议.EventHandleName只是一个常量,封装在进程1和进程2都引用的DLL中。您不需要这样做,但它可以防止您键入错误的名称并导致死锁。

    AutoResetEvent只能是本地的。但你说得对——EventWaitHandle正是我想要的。谢谢为什么不能将AutoResetEvent和ManualResetEvent用于进程间通信?它们都源自EventWaitHandle