/// <summary> /// Waits for a task to stop running (must be in disabled state) /// </summary> /// <param name="t"></param> /// <param name="maxWaitMilliseconds"></param> protected void DisableAndStopTask(Task t, int maxWaitMilliseconds = 30000) { this.Logger.LogInfo(true, "Stopping scheduler task {0} with state {1}", t.Name, t.State); t.Enabled = false; bool hasStopped = UtilsSystem.WaitWhile( () => t.State == TaskState.Running, maxWaitMilliseconds, $"Waiting for task {t.Name} to stop running...", this.Logger); // The task did not stop by itself, so we need to forcefully close it. if (!hasStopped) { this.Logger.LogInfo(true, "Forcefully stopping task {0} ", t.Name); t.Stop(); } // Wait again hasStopped = UtilsSystem.WaitWhile( () => t.State == TaskState.Running, maxWaitMilliseconds, $"Waiting for task {t.Name} to stop...", this.Logger); if (!hasStopped) { this.Logger.LogWarning(false, "Could not stop scheduled task {0}", t.Name); } }