简体   繁体   English

结合ManualResetEvent和token.WaitHandle.WaitOne

[英]Combine ManualResetEvent and token.WaitHandle.WaitOne

I got: 我有:

internal void Start(CancellationToken token)
{
   while (!token.IsCancellationRequested)
   {
       //do work
       token.WaitHandle.WaitOne(TimeSpan.FromSeconds(67));
   }       
}

So i start this method in new Task and do some work in loop untill i need to cancel it with token Some times i need to force new loop iteration, without waiting this 67 seconds. 所以我在新的Task启动这个方法,并在循环中做一些工作,直到我需要用token取消它有时我需要强制新的循环迭代,而不是等待这67秒。 I think i need something like: 我想我需要这样的东西:

public ManualResetEvent ForceLoopIteration { get; set; }

Meanwhile i cant understand how to use it with token. 同时我无法理解如何使用令牌。 Maybe something like WaitHandle.WaitAny() ? 也许像WaitHandle.WaitAny()

Youre on the right way, try this: 你是正确的方式,试试这个:

WaitHandle.WaitAny(
    new[] { token.WaitHandle, ForceLoopIteration },
    TimeSpan.FromSeconds(67));

This waits for the occurence of one of the following 这等待出现以下之一

  • cancelation is requested on token token上请求取消
  • ForceLoopIteration is set ForceLoopIteration已设置
  • timeout of 67 seconds has been elapsed 已超过67秒的超时时间

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM