gpt4 book ai didi

c# - 如何知道是否调用了 EventWaitHandle.Set

转载 作者:行者123 更新时间:2023-11-30 20:59:56 31 4
gpt4 key购买 nike

有没有办法知道调用了哪个EventWaitHandle

我有两个带有 2 个不同系统范围事件名称的自定义类。

我有它们的原因是为了区分触发哪个函数。

我现在的问题是如何区分触发了哪个事件?

EventWaitHandle _ew1 = new EventWaitHandle(false, EventResetMode.AutoReset, "Mode1");
EventWaitHandle _ew2 = new EventWaitHandle(false, EventResetMode.AutoReset, "Mode2");

因此,如果 _ew1.Set() 被调用,那么我需要执行 Process1

如果 _ew2.Set() 被调用,那么我需要执行 Process2

更新:添加了更多信息。

主线程是一个windows服务。由 Web 应用程序和桌面应用程序发出信号。所以基本上服务需要识别谁从 Web 应用程序或桌面应用程序触发了事件,如果它是 WebAppliation,则执行 SP1,否则,如果它是 Windows 应用程序,则执行 SP2。

最佳答案

想法1

WaitHandle.WaitAny静态方法返回信号等待句柄的索引,因此最简单的解决方案是检查该索引。

例子

static class Program
{
private static Random _random = new Random();
private static AutoResetEvent[] _eventHandles = new[] {new AutoResetEvent(false), new AutoResetEvent(false)};

static void Main()
{
Thread[] threads = new Thread[10];

for (int i = 0; i < threads.Length; i++)
{
threads[i] = new Thread(Method);
threads[i].Start();

var handleIndex = WaitHandle.WaitAny(_eventHandles);
Console.WriteLine(handleIndex == 0 ? "Process1" : "Process2");
}
}

static void Method()
{
if (_random.Next()%2 == 0)
_eventHandles[0].Set();
else
_eventHandles[1].Set();
}
}

想法2

您还可以使用一个事件句柄和一个可变字段,该字段将指示已满足哪些条件语句,以便在发出信号后执行适当的过程。

例子

enum Process
{
Process1,
Process2
}

static class Program
{
private static Random _random = new Random();

private static AutoResetEvent _eventHandle = new AutoResetEvent(false);
private static volatile Process _selectedProcess = Process.Process1;

static void Main()
{
Thread[] threads = new Thread[10];

for (int i = 0; i < threads.Length; i++)
{
threads[i] = new Thread(Method);
threads[i].Start();

_eventHandle.WaitOne();

Console.WriteLine(_selectedProcess == Process.Process1 ? "Process1" : "Process2");
}
}

static void Method()
{
_selectedProcess = _random.Next()%2 == 0 ? Process.Process1 : Process.Process2;
_eventHandle.Set();
}
}

想法3

如果您无法修改外部组件并且您只有事件句柄,那么您可以尝试为每个选项启动新线程并在那里等待相应的信号以执行适当的操作。

例子

static class Program
{
private static Random _random = new Random();
private static AutoResetEvent[] _eventHandles = new[] {new AutoResetEvent(false), new AutoResetEvent(false)};

static void Main()
{
Thread[] processThreads = new Thread[2];

processThreads[0] = new Thread(Process1);
processThreads[0].Start();

processThreads[1] = new Thread(Process2);
processThreads[1].Start();


Thread[] threads = new Thread[10];

for (int i = 0; i < threads.Length; i++)
{
threads[i] = new Thread(Method);
threads[i].Start();
}
}

static void Method()
{
if (_random.Next()%2 == 0)
_eventHandles[0].Set();
else
_eventHandles[1].Set();
}

static void Process1()
{
while (true)
{
_eventHandles[0].WaitOne();
Console.WriteLine("Process1");
}
}

static void Process2()
{
while (true)
{
_eventHandles[1].WaitOne();
Console.WriteLine("Process2");
}
}
}

想法4

如果流程需要少量时间,您可以使用 ThreadPool.RegisterWaitForSingleObject Method

例子

static class Program
{
private static Random _random = new Random();
private static AutoResetEvent[] _eventHandles = new[] {new AutoResetEvent(false), new AutoResetEvent(false)};

static void Main()
{
ThreadPool.RegisterWaitForSingleObject(_eventHandles[0], Process1, null, Timeout.Infinite, false);
ThreadPool.RegisterWaitForSingleObject(_eventHandles[1], Process2, null, Timeout.Infinite, false);


Thread[] threads = new Thread[10];

for (int i = 0; i < threads.Length; i++)
{
threads[i] = new Thread(Method);
threads[i].Start();
}
}

static void Method()
{
if (_random.Next()%2 == 0)
_eventHandles[0].Set();
else
_eventHandles[1].Set();
}

static void Process1(object state, bool timedOut)
{
Console.WriteLine("Process1");
}

static void Process2(object state, bool timedOut)
{
Console.WriteLine("Process2");
}
}

关于c# - 如何知道是否调用了 EventWaitHandle.Set,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15241808/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com