diff --git a/OpenNest.Desktop.Tests/Forms/OperationProgressTests.cs b/OpenNest.Desktop.Tests/Forms/OperationProgressTests.cs new file mode 100644 index 00000000..f5822aca --- /dev/null +++ b/OpenNest.Desktop.Tests/Forms/OperationProgressTests.cs @@ -0,0 +1,35 @@ +using System; +using System.Threading; +using System.Threading.Tasks; +using OpenNest.Forms; + +namespace OpenNest.Desktop.Tests.Forms; + +public class OperationProgressTests +{ + [Theory] + [InlineData(0, false, "Elapsed 0:00")] + [InlineData(65.9, false, "Elapsed 1:05")] + [InlineData(3601, true, "Canceling... waiting for the current step to stop (60:01)")] + public void Status_text_shows_the_elapsed_minutes_and_seconds(double seconds, bool canceling, string expected) => + Assert.Equal(expected, OperationProgress.StatusText(TimeSpan.FromSeconds(seconds), canceling)); + + [Fact] + public void A_finished_or_faulted_run_finishes_within_the_delay() + { + Assert.True(OperationProgress.FinishesWithin(Task.CompletedTask, TimeSpan.Zero)); + Assert.True(OperationProgress.FinishesWithin(Task.FromException(new InvalidOperationException()), TimeSpan.Zero)); + } + + [Fact] + public void A_run_still_going_does_not_finish_within_the_delay() + { + using var release = new ManualResetEventSlim(); + var task = Task.Run(release.Wait); + + Assert.False(OperationProgress.FinishesWithin(task, TimeSpan.FromMilliseconds(20))); + + release.Set(); + Assert.True(OperationProgress.FinishesWithin(task, TimeSpan.FromSeconds(10))); + } +} diff --git a/OpenNest.Desktop/Forms/OperationProgress.cs b/OpenNest.Desktop/Forms/OperationProgress.cs new file mode 100644 index 00000000..d8f0bd57 --- /dev/null +++ b/OpenNest.Desktop/Forms/OperationProgress.cs @@ -0,0 +1,41 @@ +using System; +using System.Threading.Tasks; + +namespace OpenNest.Forms +{ + /// + /// The rules of the small "working" window both desktop apps show with a Cancel button while + /// a long plate command (Expand Spacing, Fix Spacing) runs on a worker: the window appears only + /// once the run outlasts , shows the elapsed time, and after + /// Cancel says it is waiting for the worker to stop. Cancel, Escape and closing only request + /// cancellation; the window closes once the worker has actually stopped. + /// + public static class OperationProgress + { + /// Runs that finish within this delay never show the window. + public static readonly TimeSpan DefaultShowDelay = TimeSpan.FromMilliseconds(300); + + /// How often the window updates its elapsed time. + public static readonly TimeSpan StatusInterval = TimeSpan.FromMilliseconds(500); + + /// + /// Waits up to for , blocking the caller as + /// the window's owner is blocked; true when it finished, faulted or was canceled in time. + /// + public static bool FinishesWithin(Task task, TimeSpan delay) + { + ArgumentNullException.ThrowIfNull(task); + // A wait handle, unlike Task.Wait, does not throw for a faulted run. + return ((IAsyncResult)task).AsyncWaitHandle.WaitOne(delay); + } + + /// "Elapsed 1:05", or after Cancel "Canceling... waiting for the current step to stop (1:05)". + public static string StatusText(TimeSpan elapsed, bool canceling) + { + var time = $"{(int)elapsed.TotalMinutes}:{elapsed.Seconds:00}"; + return canceling + ? $"Canceling... waiting for the current step to stop ({time})" + : $"Elapsed {time}"; + } + } +} diff --git a/OpenNest/Forms/OperationProgressForm.cs b/OpenNest/Forms/OperationProgressForm.cs index 11f88b76..fb0a9bec 100644 --- a/OpenNest/Forms/OperationProgressForm.cs +++ b/OpenNest/Forms/OperationProgressForm.cs @@ -16,7 +16,7 @@ namespace OpenNest.Forms; internal sealed class OperationProgressForm : Form { /// Runs that finish within this delay never show the window. - internal static readonly TimeSpan DefaultShowDelay = TimeSpan.FromMilliseconds(300); + internal static readonly TimeSpan DefaultShowDelay = OperationProgress.DefaultShowDelay; private readonly Task task; private readonly CancellationTokenSource cts; @@ -78,7 +78,7 @@ internal sealed class OperationProgressForm : Form CancelButton = cancelButton; cancelButton.DialogResult = DialogResult.None; - elapsedTimer = new System.Windows.Forms.Timer { Interval = 500 }; + elapsedTimer = new System.Windows.Forms.Timer { Interval = (int)OperationProgress.StatusInterval.TotalMilliseconds }; elapsedTimer.Tick += (_, _) => UpdateStatus(); UpdateStatus(); @@ -112,8 +112,7 @@ internal sealed class OperationProgressForm : Form var token = cts.Token; var task = Task.Run(() => work(token)); - // A wait handle, unlike Task.Wait, does not throw for a faulted run. - if (!((IAsyncResult)task).AsyncWaitHandle.WaitOne(showDelay ?? DefaultShowDelay)) + if (!OperationProgress.FinishesWithin(task, showDelay ?? DefaultShowDelay)) { using var form = new OperationProgressForm(title, message, task, cts); form.ShowDialog(owner); @@ -185,10 +184,6 @@ internal sealed class OperationProgressForm : Form private void UpdateStatus() { - var elapsed = stopwatch.Elapsed; - var time = $"{(int)elapsed.TotalMinutes}:{elapsed.Seconds:00}"; - statusLabel.Text = cts.IsCancellationRequested - ? $"Canceling... waiting for the current step to stop ({time})" - : $"Elapsed {time}"; + statusLabel.Text = OperationProgress.StatusText(stopwatch.Elapsed, cts.IsCancellationRequested); } }