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 { } } }
/// <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); } }