private void ExecuteInternal(Action <CancellationToken, IProgress <string> > action, ProgressDialogOptions options, bool isCancellable = true) { if (action == null) { throw new ArgumentNullException("action"); } if (options == null) { throw new ArgumentNullException("options"); } using (var cancellationTokenSource = new CancellationTokenSource()) { CancellationToken cancellationToken = cancellationTokenSource.Token; var cancelCommand = isCancellable ? new CancelCommand(cancellationTokenSource) : null; var viewModel = new ProgressDialogWindowViewModel( options, cancellationToken, cancelCommand); var window = new ProgressDialogWindow { DataContext = viewModel }; var task = taskFactory .StartNew(() => action(cancellationToken, viewModel.Progress), cancellationToken); task.ContinueWith(_ => viewModel.Close = true); window.ShowDialog(); } }
private bool TryExecuteInternal <T>( Func <CancellationToken, IProgress <string>, T> action, ProgressDialogOptions options, out T result, bool isCancellable = true) { if (action == null) { throw new ArgumentNullException("action"); } if (options == null) { throw new ArgumentNullException("options"); } using (var cancellationTokenSource = new CancellationTokenSource()) { CancellationToken cancellationToken = cancellationTokenSource.Token; var cancelCommand = isCancellable ? new CancelCommand(cancellationTokenSource) : null; var viewModel = new ProgressDialogWindowViewModel( options, cancellationToken, cancelCommand); var window = new ProgressDialogWindow { DataContext = viewModel }; var task = taskFactory .StartNew(() => action(cancellationToken, viewModel.Progress), cancellationToken); task.ContinueWith(_ => viewModel.Close = true); window.ShowDialog(); if (task.IsCanceled) { result = default(T); return(false); } if (task.IsCompleted) { result = task.Result; return(true); } result = default(T); return(false); } }
async private Task ExecuteAsyncInternal( Func <CancellationToken, IProgress <string>, Task> action, ProgressDialogOptions options, bool isCancellable = true) { if (action == null) { throw new ArgumentNullException("action"); } if (options == null) { throw new ArgumentNullException("options"); } using (var cancellationTokenSource = new CancellationTokenSource()) { CancellationToken cancellationToken = cancellationTokenSource.Token; var cancelCommand = isCancellable ? new CancelCommand(cancellationTokenSource) : null; var viewModel = new ProgressDialogWindowViewModel( options, cancellationToken, cancelCommand); var window = new ProgressDialogWindow { DataContext = viewModel }; Task task = action(cancellationToken, viewModel.Progress); task.ContinueWith(_ => viewModel.Close = true); window.ShowDialog(); await task; } }