async Task StartDispatcherTask() { try { do { try { await signal.WaitHandle.WaitOneAsync(tokenSource.Token).ConfigureAwait(false); signal.Reset(); } catch (OperationCanceledException) { break; } await DispatchEvents(tokenSource.Token).ConfigureAwait(false); } while (!tokenSource.IsCancellationRequested); } catch (Exception ex) { Logger.Error("An exception occurred when dispatching external integration events", ex); circuitBreaker.Failure(ex); if (!tokenSource.IsCancellationRequested) { StartDispatcher(); } } }
void StartThread() { var token = tokenSource.Token; Task.Factory .StartNew(TryProcessMessage, token, token, TaskCreationOptions.LongRunning, TaskScheduler.Default) .ContinueWith(t => { if (t.Exception != null) { t.Exception.Handle(ex => { circuitBreaker.Failure(ex); return(true); }); } StartThread(); }, TaskContinuationOptions.OnlyOnFaulted); }