///<summary>Executes a cancellable asynchronous operation and displays its progress.</summary> ///<param name="parent">The form that will own the progress display. This parameter can be null.</param> ///<param name="method">The method to execute. This should not do any immediate blocking work.</param> ///<returns>False if the cancel button was clicked.</returns> public static async Task ExecuteAsync(IWin32Window parent, Func <IProgressReporter, CancellationToken, Task> method) { if (method == null) { throw new ArgumentNullException("method"); } using (var dialog = new ProgressForm() { CanCancel = true }) { dialog.FadeIn(); try { var task = method(dialog, dialog.CancellationSource.Token); await Task.WhenAny(task, Task.Delay(5)); // If it finishes quickly, don't bother showing the dialog. if (task.IsCompleted) { return; } SynchronizationContext.Current.Post(_ => dialog.ShowDialog(parent), null); await task; } finally { dialog.Finished = true; dialog.FadeOut(); } if (dialog.WasCanceled) { throw new TaskCanceledException(); } } }
public static bool Execute(IWin32Window parent, Action<IProgressReporter> method, bool cancellable) { if (method == null) throw new ArgumentNullException("method"); Exception exception = null; bool canceled = false; using (var dialog = new ProgressForm() { CanCancel = cancellable }) { dialog.FadeIn(); ThreadPool.QueueUserWorkItem(delegate { try { method(dialog); } catch (Exception ex) { exception = ex; } finally { dialog.Finished = true; dialog.FadeOut(); } canceled = dialog.WasCanceled; }); if (!dialog.IsDisposed && !dialog.Finished) dialog.ShowDialog(parent); } if (exception != null) throw new TargetInvocationException(exception); return !canceled; }
///<summary>Executes a cancellable asynchronous operation and displays its progress.</summary> ///<param name="parent">The form that will own the progress display. This parameter can be null.</param> ///<param name="method">The method to execute. This should not do any immediate blocking work.</param> ///<returns>False if the cancel button was clicked.</returns> public static async Task ExecuteAsync(IWin32Window parent, Func<IProgressReporter, CancellationToken, Task> method) { if (method == null) throw new ArgumentNullException("method"); using (var dialog = new ProgressForm() { CanCancel = true }) { dialog.FadeIn(); try { var task = method(dialog, dialog.CancellationSource.Token); await Task.WhenAny(task, Task.Delay(5)); // If it finishes quickly, don't bother showing the dialog. if (task.IsCompleted) return; SynchronizationContext.Current.Post(_ => dialog.ShowDialog(parent), null); await task; } finally { dialog.Finished = true; dialog.FadeOut(); } if (dialog.WasCanceled) throw new TaskCanceledException(); } }
public static bool Execute(IWin32Window parent, Action <IProgressReporter> method, bool cancellable) { if (method == null) { throw new ArgumentNullException("method"); } Exception exception = null; bool canceled = false; using (var dialog = new ProgressForm() { CanCancel = cancellable }) { dialog.FadeIn(); ThreadPool.QueueUserWorkItem(delegate { try { method(dialog); } catch (Exception ex) { exception = ex; } finally { dialog.Finished = true; dialog.FadeOut(); } canceled = dialog.WasCanceled; }); if (!dialog.IsDisposed && !dialog.Finished) { dialog.ShowDialog(parent); } } if (exception != null) { throw new TargetInvocationException(exception); } return(!canceled); }