public void AddTaskToQueue(LongRunningTask task) { if (task == null) { throw new ArgumentNullException(nameof(task)); } logger.LogDebug($"Added task \"{task.Title}\" to queue."); lock (syncLock) { if (tasks.Contains(task)) { return; } tasks = tasks.Add(task); task.CompletionTask.ContinueWith(t => OnCompletedTask(t.Result)); } }
private void OnCompletedTask(LongRunningTask task) { lock (syncLock) { // remove completed task tasks = tasks.Remove(task); if (!task.IsCompleted) { throw new InvalidOperationException("Task is unfinished."); } // check result and add to completed if (task.IsCompletedSuccessfully) { logger.LogInformation($"Task \"{task.Title}\" completed successfully. Has been running for {task.Elapsed}."); } else { logger.LogError(task.FaultException, $"Task \"{task.Title}\" (has been running for {task.Elapsed}): {task.ProgressText ?? "Unknown reason"}"); } completedTasks = completedTasks.Insert(index: 0, item: task); } }