/// <summary> /// Registers a task to be run. /// </summary> /// <param name="task">The task to register.</param> private static void RegisterTask(BootTask task) { task.Task = new Task(new Action(task.Run), CancellationToken.Token); UnstartedTasks.Add(task); TotalTasks++; }
/// <summary> /// Marks a given task as finished. /// </summary> /// <param name="task">The task to finish.</param> public static void Finish(BootTask task) { if (UnfinishedTasks.Contains(task)) { UnfinishedTasks.Remove(task); FinishedTasks++; } }
/// <summary> /// Updates the status of currently running tasks. /// </summary> public static void Update() { foreach (BootTask task in UnfinishedTasks.ToArray()) { if (task.Task.IsFaulted) { // Task threw an error. HasErrored = true; Logger.Log(LogLevel.Error, $"A startup task encountered an error."); Logger.LogException(task.Task.Exception); return; } } // If startup has errored - do not continue. if (HasErrored) { CancellationToken.Cancel(); return; } // Start new tasks if (UnstartedTasks.Count > 0) { if (UnfinishedTasks.Count < Settings.MaxStartupThreads) { int TasksToStart = Settings.MaxStartupThreads - UnfinishedTasks.Count; while (TasksToStart > 0 && UnstartedTasks.Count > 0) { BootTask newTask = UnstartedTasks[0]; UnfinishedTasks.Add(newTask); UnstartedTasks.Remove(newTask); newTask.Task.Start(); } } } // Finished loading - all tasks completed if (UnstartedTasks.Count == 0 && UnfinishedTasks.Count == 0) { FinishedFirstStage = true; CurrentlyStarting = false; } }