/// <summary> /// Stops the gurdian. /// </summary> /// <returns></returns> public static bool StopGuardian(bool supressMessage = false) { try { _guardianIsRunning = false; _taskPool.Clear(); _upcomingTasks.Clear(); if (!supressMessage) { GuardianStopped?.Invoke("Guardian has been manually stopped."); } return(true); } catch (Exception ex) { GuardianThrewException?.Invoke(ex, "Couldn't stop the guardian."); return(false); } }
/// <summary> /// The continous thread that stays in the background and completes task after task. /// </summary> private static void GuardianThread() { lock (_lock) { try { while (_guardianIsRunning) { CheckUpcomingTasks(); if (_upcomingTasks.Count > 0) { // Take the next possible task var nextTask = _upcomingTasks.FirstOrDefault(t => t.ExecutionDate <= DateTime.Now); if (nextTask != default) { //Handle the task now! if (_taskPool.TryGetValue(nextTask, out var task)) { HandleTask(task); } else { ;// Inform someone that a task is missing. } } } // Maybe let the user decide the frequency of the Guardian later. Thread.Sleep(1000); } } catch (Exception ex) { GuardianThrewException?.Invoke(ex, ex.Message); GuardianStopped?.Invoke($"The guardian detected an error and has been shut down. " + $"After the error has been fixed, try to restart him manually via the Home-Hub. {Environment.NewLine}" + $"Error: {ex.Message}"); } } }