private async Task AsyncWorker() { isWorkerRunning = true; while (IsRunning) { //Wait to be signalled await asyncResetEvent.WaitAsync(PeriodicSignallingTime); //Ensure we should still be running if (!IsRunning) { break; } //Check if we should be running now. If so, we wait, but don't break out of the loop if (checkForContinueMethod != null && !checkForContinueMethod()) { continue; } try { await workerMethod(workerState); } catch (Exception ex) { TraceQueue.Trace(this, TracingLevel.Warning, "{0} occurred while running worker method: {1}\r\n\r\n{2}", ex.GetType().Name, ex.Message, ex.StackTrace); } } //Signal completion isWorkerRunning = false; }
public async Task IntervalTest() { var tcs = new TaskCompletionSource<bool>(); var resetEvent = new AsyncAutoResetEvent(); //Timeout in 100ms Task timeoutTask = resetEvent.WaitAsync(TimeSpan.FromMilliseconds(100)).ContinueWith((r) => tcs.TrySetResult(true)); Task testTimeoutTask = Task.Delay(1000).ContinueWith((r) => tcs.TrySetResult(false)); bool success = await tcs.Task; Assert.IsTrue(success, "Timeout for async reset event failed to fire"); }
public async Task IntervalTest() { var tcs = new TaskCompletionSource <bool>(); var resetEvent = new AsyncAutoResetEvent(); //Timeout in 100ms Task timeoutTask = resetEvent.WaitAsync(TimeSpan.FromMilliseconds(100)).ContinueWith((r) => tcs.TrySetResult(true)); Task testTimeoutTask = Task.Delay(1000).ContinueWith((r) => tcs.TrySetResult(false)); bool success = await tcs.Task; Assert.IsTrue(success, "Timeout for async reset event failed to fire"); }