private async Task StartTask() { while (true) { try { try { var task = Dequeue(); var value = await task.Task; if (value == null) { return; } await Consume(value, _cancellation.Token).ConfigureAwait(false); if (_isAppInisghtsMetricEnabled) { ApplicationInsightsTelemetry.TrackMetric(_metricName, _queue.Count); } } catch (Exception exception) { await LogErrorAsync(exception); } } // Saves the loop if nothing didn't help // ReSharper disable once EmptyGeneralCatchClause catch { } } }
private async Task ThreadMethod(CancellationToken cancellation) { while (!cancellation.IsCancellationRequested) { try { if (_isTelemetryDisabled) { try { await Execute(cancellation).ConfigureAwait(false); } catch (Exception exception) { LogFatalError(exception); } } else { var telemtryOperation = ApplicationInsightsTelemetry.StartRequestOperation($"{nameof(TimerPeriod)} on {_typeName} for {_componentName}"); try { await Execute(cancellation).ConfigureAwait(false); } catch (Exception exception) { LogFatalError(exception); ApplicationInsightsTelemetry.MarkFailedOperation(telemtryOperation); ApplicationInsightsTelemetry.TrackException(exception); } finally { ApplicationInsightsTelemetry.StopOperation(telemtryOperation); } } try { await Task.Delay(_periodMs, cancellation).ConfigureAwait(false); } catch (TaskCanceledException) { } } // Saves the loop if nothing didn't help // ReSharper disable once EmptyGeneralCatchClause catch { } } }
/// <summary> /// Produces next item. If producer-consumer is not started yet, then it will be started automatically /// </summary> protected void Produce(T item) { Start(); lock (_queue) { var last = _last; _last = new TaskCompletionSource <T>(); _queue.Enqueue(_last); if (_isAppInisghtsMetricEnabled) { ApplicationInsightsTelemetry.TrackMetric(_metricName, _queue.Count); } last.SetResult(item); } }