等待EventWaitHandle.Set()之后通知所有进程

本文关键字:通知 进程 之后 EventWaitHandle Set 等待 | 更新日期: 2023-09-27 18:07:13

我在一个过程中完成

_eventWaitHandle.Set();
_eventWaitHandle.Reset();

在另一个过程中

_eventWaitHandle.WaitOne();
Console.WriteLine("Hello");

但永远不会收到通知(没有控制台输出(。看来Set是异步的。

在执行Reset((之前,如何等待所有服务员都收到信号

我创建等待句柄(NAMED进程间等待句柄(:

    internal static EventWaitHandle OpenExistingOrCreateEventWaitHandle(string name)
    {
        try
        {
            return EventWaitHandle.OpenExisting(name);
        }
        catch (WaitHandleCannotBeOpenedException)
        {
            return new EventWaitHandle(false, EventResetMode.ManualReset, name);
        }
    }

更新

现在我有一个"解决方案">

_eventWaitHandle.Set();
Thread.Sleep(10);
_eventWaitHandle.Reset();

第二种可能——每个进程都有许多句柄。但这段代码应该适用于任何office应用程序外接程序或独立应用程序。所以名字应该产生一些方式和发现。

第三,使用WCF p2p(netPeerTcpBinding(或带有UdpDiscoveryEndpoint的命名管道,但这些管道使用"IP",因此在部署到最终用户时可能会出现一些安全问题?

等待EventWaitHandle.Set()之后通知所有进程

是的,Set()函数会立即退出,所以像这样调用Set()Reset()基本上什么都不做,或者随机地做一些事情。您可以通过在WaitOne()之后重置侦听线程上的事件来解决此问题。

您可以将EventResetMode设置为EventResetMode.AutoReset,这样,当其中一个进程处理该事件时,它将自动重置该事件。之后您不必手动重置它。

有了多个进程,您可以为每个侦听器创建一个事件,并在必须发出事件信号时触发所有侦听器。

foreach(var process in _myProcesses)
{
    waitHandles.Add(OpenExistingOrCreateEventWaitHandle(process.SharedWaitHandleName);
}
...
internal static EventWaitHandle OpenExistingOrCreateEventWaitHandle(string name)
{
    try
    {
        return EventWaitHandle.OpenExisting(name);
    }
    catch (WaitHandleCannotBeOpenedException)
    {
        return new EventWaitHandle(false, EventResetMode.AutoReset, name);
    }
}

...

foreach(var waitHandle in waitHandles)
{
    waitHandle.Set();
}

如果您想知道如何在执行Reset((之前等待所有等待线程,其中等待线程是同一进程中的不同线程,请从EventWaitHandle类的MSDN页面中查看此示例

using System;
using System.Threading;
public class Example
{
    // The EventWaitHandle used to demonstrate the difference
    // between AutoReset and ManualReset synchronization events.
    //
    private static EventWaitHandle ewh;
    // A counter to make sure all threads are started and
    // blocked before any are released. A Long is used to show
    // the use of the 64-bit Interlocked methods.
    //
    private static long threadCount = 0;
    // An AutoReset event that allows the main thread to block
    // until an exiting thread has decremented the count.
    //
    private static EventWaitHandle clearCount = 
        new EventWaitHandle(false, EventResetMode.AutoReset);
    [MTAThread]
    public static void Main()
    {
        // Create an AutoReset EventWaitHandle.
        //
        ewh = new EventWaitHandle(false, EventResetMode.AutoReset);
        // Create and start five numbered threads. Use the
        // ParameterizedThreadStart delegate, so the thread
        // number can be passed as an argument to the Start 
        // method.
        for (int i = 0; i <= 4; i++)
        {
            Thread t = new Thread(
                new ParameterizedThreadStart(ThreadProc)
            );
            t.Start(i);
        }
        // Wait until all the threads have started and blocked.
        // When multiple threads use a 64-bit value on a 32-bit
        // system, you must access the value through the
        // Interlocked class to guarantee thread safety.
        //
        while (Interlocked.Read(ref threadCount) < 5)
        {
            Thread.Sleep(500);
        }
        // Release one thread each time the user presses ENTER,
        // until all threads have been released.
        //
        while (Interlocked.Read(ref threadCount) > 0)
        {
            Console.WriteLine("Press ENTER to release a waiting thread.");
            Console.ReadLine();
            // SignalAndWait signals the EventWaitHandle, which
            // releases exactly one thread before resetting, 
            // because it was created with AutoReset mode. 
            // SignalAndWait then blocks on clearCount, to 
            // allow the signaled thread to decrement the count
            // before looping again.
            //
            WaitHandle.SignalAndWait(ewh, clearCount);
        }
        Console.WriteLine();
        // Create a ManualReset EventWaitHandle.
        //
        ewh = new EventWaitHandle(false, EventResetMode.ManualReset);
        // Create and start five more numbered threads.
        //
        for(int i=0; i<=4; i++)
        {
            Thread t = new Thread(
                new ParameterizedThreadStart(ThreadProc)
            );
            t.Start(i);
        }
        // Wait until all the threads have started and blocked.
        //
        while (Interlocked.Read(ref threadCount) < 5)
        {
            Thread.Sleep(500);
        }
        // Because the EventWaitHandle was created with
        // ManualReset mode, signaling it releases all the
        // waiting threads.
        //
        Console.WriteLine("Press ENTER to release the waiting threads.");
        Console.ReadLine();
        ewh.Set();
    }
    public static void ThreadProc(object data)
    {
        int index = (int) data;
        Console.WriteLine("Thread {0} blocks.", data);
        // Increment the count of blocked threads.
        Interlocked.Increment(ref threadCount);
        // Wait on the EventWaitHandle.
        ewh.WaitOne();
        Console.WriteLine("Thread {0} exits.", data);
        // Decrement the count of blocked threads.
        Interlocked.Decrement(ref threadCount);
        // After signaling ewh, the main thread blocks on
        // clearCount until the signaled thread has 
        // decremented the count. Signal it now.
        //
        clearCount.Set();
    }
}

我解决了这个问题。我使用内存映射文件来存储事件等待句柄名称的列表。超时无法稳定工作。目前的解决方案在2年内投入生产。

为了有类似p2p的IPC桌面事件,我使用了下一个收据:

  • 1个共享互斥
  • 每个流程(事件参与者(有1个唯一的服务员和1个唯一响应者事件等待句柄
  • 1个内存映射文件,用于存储等待的参与者的注册表(可以使用实际注册表(
  • 交换事件数据的1个内存映射文件

使用EventResetMode.Manual并将EventWaitHandle存储在静态中。消费者应该从此静态中读取句柄。每当您要调用Set((时,首先创建一个新的EventWaitHandle并将其存储在该静态中。下次消费者想要手柄时,他会得到新的,这是显而易见的。