protected async override Task ExecuteAsync( CancellationToken cancellationToken) { _logger.LogInformation("Queued Hosted Service is starting."); BaseTask workItem = null; while (!cancellationToken.IsCancellationRequested) { if (this.GetValueCounter() > _maxDequeueCountPerMin) // allow start 80 action per minute { string msg = string.Format("SKIPPED queued action counter: {0}. In Queue: {1}. MaxCounter: {2}.", _counter, TaskQueue.Count(), _maxDequeueCountPerMin); _logger.LogInformation(msg); Console.WriteLine(msg); Thread.Sleep(1000 * 10); } else { var t = await TaskQueue.Dequeue(); t.Start(); this.IncreaseCounter(1); string msg = string.Format("DEQUEUE Queued Hosted Service count: {0}. In Queue: {1}. MaxCounter: {2}.", _counter, TaskQueue.Count(), _maxDequeueCountPerMin); _logger.LogInformation(msg); Console.WriteLine(msg); } } _logger.LogInformation("Queued Hosted Service is stopping."); }
public void QueueBackgroundWorkItem(BaseTask workItem) { if (workItem == null) { throw new ArgumentNullException(nameof(workItem)); } Console.WriteLine("WorkItem Enqueue. TaskId: {0}", workItem.TaskID); _workItems.Enqueue(workItem); _signal.Release(); }