문제

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. 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()?

도움이 되었습니까?

해결책

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
  • ForceLoopIteration is set
  • timeout of 67 seconds has been elapsed
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top