gpt4 book ai didi

c# - 条件事件等待/ManualResetEvent

转载 作者:行者123 更新时间:2023-11-30 15:03:15 25 4
gpt4 key购买 nike

我知道如何使用 ManualResetEvent 或同步原语(如 Monitor)来等待事件和/或锁定,但我想知道是否有一种方法可以实现如下内容:

ManualResetEvent resetEvent; 

public string WaitForOneThousandMs()
{
resetEvent.Wait(1000);

if (WaitTime(resetEvent) <= 1000)
return "Event occured within 1000ms.";
else
return "Event did not occur within 1000ms.";
}

1)等待1000ms事件X发生

2)如果事件在1000ms内发生,执行路径A

3) 否则,执行路径B

这基本上是一个条件等待函数,条件是我们必须等待多长时间,如果可能的话,最好的实现方式是什么?

最佳答案

看起来你在追求:

return resetEvent.WaitOne(1000) ? "Event occurred within 1000ms"
: "Event did not occur within 1000ms";

来自 WaitHandle.WaitOne 的文档:

Return value
true if the current instance receives a signal; otherwise, false.

Monitor.Wait以类似的方式返回 bool

关于c# - 条件事件等待/ManualResetEvent,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11783520/

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