我得到了:
internal void Start(CancellationToken token)
{
while (!token.IsCancellationRequested)
{
//do work
token.WaitHandle.WaitOne(TimeSpan.FromSeconds(67));
}
}
所以我在新的 Task
中开始这个方法并在循环中做一些工作直到我需要用 token
取消它
有时我需要强制执行新的循环迭代,而无需等待这 67 秒。
我想我需要这样的东西:
public ManualResetEvent ForceLoopIteration { get; set; }
与此同时,我无法理解如何将它与 token 一起使用。
也许像 WaitHandle.WaitAny()
这样的东西?
最佳答案
你走对了,试试这个:
WaitHandle.WaitAny(
new[] { token.WaitHandle, ForceLoopIteration },
TimeSpan.FromSeconds(67));
这等待以下情况之一的发生
- 请求取消
token
ForceLoopIteration
已设置- 67 秒超时时间已过
关于c# - 结合 ManualResetEvent 和 token.WaitHandle.WaitOne,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23652224/