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()
?
WaitOne(TimeSpan, Boolean) Blocks the current thread until the current instance receives a signal, using a TimeSpan to specify the time interval and specifying whether to exit the synchronization domain before the wait. public: virtual bool WaitOne(TimeSpan timeout, bool exitContext); C# Copy.
An AutoResetEvent resets when the code passes through event. WaitOne() , but a ManualResetEvent does not.
static WaitHandle[] waitHandles = new WaitHandle[] { new AutoResetEvent(false), new AutoResetEvent(false) }; // Define a random number generator for testing. static Random r = new Random(); static void Main() { // Queue up two tasks on two different threads; // wait until all tasks are completed.
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
token
ForceLoopIteration
is setIf you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With