同步使用的进程间的同步2进程中的对象 - 或互斥的AutoResetEvent [英] Synchronizing 2 processes using interprocess synchronizations objects - Mutex or AutoResetEvent

查看:229
本文介绍了同步使用的进程间的同步2进程中的对象 - 或互斥的AutoResetEvent的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑以下情形:我跑我的应用程序当中,它的执行过程中,必须运行另一个进程,并在那之后第二个过程完成后内特定的初始化,可我的第一个进程继续下去。例如:

Consider the following scenario: I'm running my application which, during its execution, has to run another process and only after that 2nd process finishes inner specific initialization, can my first process continue. E.g:

...
// Process1 code does various initializations here
Process.Start("Process2.exe");
// Wait until Process2 finishes its initialization and only then continue (Process2 doesn't exit)
...

我看到几个选项:

I see several options:

  1. 互斥 - 考虑进程间通信时互斥想到自动,不过,我看不到导致处理1,等待他产生自己是互斥的一种方式。我可能会导致过程2创建一个互斥体,并直至创建互斥等待处理1(使用轮询和Mutex.OpenExisting功能)
  2. 的AutoResetEvent - 这些将是完美的任务,然而,似乎是在.NET中,不能用于进程间通信电子
  3. CreateEvent的 - 我可以使用P /调用和使用的Win32 CreateEvent的功能。从理论上讲,它可以为我提供了我需要的一切。 ,我宁愿不使用本机的功能,但如果可能的话。
  4. 使用外部文件 - 最简单的方法是只使用一些操作系统的外部对象(文件,注册表等)。然而,然而,这似乎是相当哈克。
  1. Mutex - Mutex comes to mind automatically when considering interprocess communication, however, I can't see a way of causing Process1 to wait for a mutex that he generated himself. I can cause Process2 to create a mutex and wait on Process1 till the Mutex is created (using polling and Mutex.OpenExisting function)
  2. AutoResetEvent - Those would be perfect for the task, however, in seems that under .NET these can't be used for interprocess communcation.
  3. CreateEvent - I can use P/Invoke and use Win32 CreateEvent function. In theory, it could provide me with everything I need. I'd rather not using native functions, however, if possible.
  4. Use external file - The easiest way would be just to use some OS external object (file, registry, etc). However, this seems rather hacky, however.

我会很高兴听到你对这种情况下的意见。

I'd be happy to hear your opinion for this case.

谢谢!

推荐答案

我正要编辑这个答案,但它似乎并不正确。所以我会后我自己...

I was just going to edit this answer, but it doesn't seem correct. So I'll post my own...

按照线程C#页面,里面有很多的同步教程, 的AutoResetEvent 不能用于进程间同步。


然而,一个名为 的EventWaitHandle 可用于进程间同步。 在上面的页面,请访问创建一个跨进程的EventWaitHandle 部分。

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:

处理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 <$ C C $>。 无论您选择 EventResetMode.AutoReset EventResetMode.ManualReset 取决于您的应用程序。

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).

对于你来说,一个自动复位可能,如果你要进行初始化每次有帮助的处理1 的开始的处理2 的。

For you, an automatic reset might be helpful if you have to perform initialization for every time process 1 starts process 2.


边注: InterprocessProtocol.EventHandleName 是刚刚结束了一个DLL,两者的进程中一个恒定的1 工艺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.

这篇关于同步使用的进程间的同步2进程中的对象 - 或互斥的AutoResetEvent的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆