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}"; } } }