From 855127f32a66b11d87d63dbe0f45b7fd7003a33d Mon Sep 17 00:00:00 2001 From: Phil Scott Date: Wed, 3 Mar 2021 22:06:11 -0500 Subject: [PATCH] Changes progress task IsFinished to account for stopped tasks Previous behavior was that the only way to get a task to a finished state was to artificially set the Value to MaxValue. With this change StopTask() will also complete the task with the change that a task cannot be restarted. --- .../Widgets/Progress/ProgressTask.cs | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/src/Spectre.Console/Widgets/Progress/ProgressTask.cs b/src/Spectre.Console/Widgets/Progress/ProgressTask.cs index 1df68be..cbc6980 100644 --- a/src/Spectre.Console/Widgets/Progress/ProgressTask.cs +++ b/src/Spectre.Console/Widgets/Progress/ProgressTask.cs @@ -71,7 +71,7 @@ namespace Spectre.Console /// /// Gets a value indicating whether or not the task has finished. /// - public bool IsFinished => Value >= MaxValue; + public bool IsFinished => StopTime != null || Value >= MaxValue; /// /// Gets the percentage done of the task. @@ -107,7 +107,8 @@ namespace Spectre.Console _maxValue = maxValue; _value = 0; - _description = description?.RemoveNewLines()?.Trim() ?? throw new ArgumentNullException(nameof(description)); + _description = description?.RemoveNewLines()?.Trim() ?? + throw new ArgumentNullException(nameof(description)); if (string.IsNullOrWhiteSpace(_description)) { throw new ArgumentException("Task name cannot be empty", nameof(description)); @@ -125,13 +126,18 @@ namespace Spectre.Console { lock (_lock) { + if (StopTime != null) + { + throw new InvalidOperationException("Stopped tasks cannot be restarted"); + } + StartTime = DateTime.Now; StopTime = null; } } /// - /// Stops the task. + /// Stops and marks the task as finished. /// public void StopTask() { @@ -292,4 +298,4 @@ namespace Spectre.Console } } } -} +} \ No newline at end of file