private void ExecuteInternal(Action <CancellationToken, IProgress <ProgressReport> > action, ProgressDialogOptions options, bool isCancellable = true) { if (action == null) { throw new ArgumentNullException(nameof(action)); } if (options == null) { throw new ArgumentNullException(nameof(options)); } using (var cancellationTokenSource = new CancellationTokenSource()) { CancellationToken cancellationToken = cancellationTokenSource.Token; var cancelCommand = isCancellable ? new CancelCommand(cancellationTokenSource) : null; var viewModel = new ProgressDialogViewModel( options, cancellationToken, cancelCommand); var window = new ProgressDialog { DataContext = viewModel, }; var task = _taskFactory .StartNew(() => action(cancellationToken, viewModel.Progress), cancellationToken); task.ContinueWith(_ => viewModel.Close = true); window.ShowDialog(); } }
public async Task ExecuteAsync(Func <Task> action, ProgressDialogOptions options) { if (action == null) { throw new ArgumentNullException(nameof(action)); } await ExecuteAsyncInternal((token, progress) => action(), options); }
public bool TryExecute <T>(Func <CancellationToken, T> action, ProgressDialogOptions options, out T result) { if (action == null) { throw new ArgumentNullException(nameof(action)); } return(TryExecuteInternal((token, progress) => action(token), options, out result)); }
public void Execute(Action <IProgress <ProgressReport> > action, ProgressDialogOptions options) { if (action == null) { throw new ArgumentNullException(nameof(action)); } ExecuteInternal((token, progress) => action(progress), options); }
public void Execute(Action <CancellationToken> action, ProgressDialogOptions options) { if (action == null) { throw new ArgumentNullException(nameof(action)); } ExecuteInternal((token, progress) => action(token), options); }
public bool TryExecute <T>(Func <IProgress <ProgressReport>, T> action, ProgressDialogOptions options, out T result) { if (action == null) { throw new ArgumentNullException(nameof(action)); } return(TryExecuteInternal((token, progress) => action(progress), options, out result, isCancellable: false)); }
public void Execute( Action <CancellationToken, IProgress <ProgressReport> > action, ProgressDialogOptions options) { if (action == null) { throw new ArgumentNullException(nameof(action)); } ExecuteInternal(action, options); }
public void Execute(Action action, ProgressDialogOptions options) { if (action == null) { throw new ArgumentNullException(nameof(action)); } ExecuteInternal((token, progress) => action(), options, isCancellable: false); }
public async Task <T> ExecuteAsync <T>(Func <CancellationToken, IProgress <ProgressReport>, Task <T> > action, ProgressDialogOptions options) { if (action == null) { throw new ArgumentNullException(nameof(action)); } return(await ExecuteAsyncInternal(action, options)); }
private bool TryExecuteInternal <T>( Func <CancellationToken, IProgress <ProgressReport>, T> action, ProgressDialogOptions options, out T result, bool isCancellable = true) { if (action == null) { throw new ArgumentNullException(nameof(action)); } if (options == null) { throw new ArgumentNullException(nameof(options)); } using (var cancellationTokenSource = new CancellationTokenSource()) { CancellationToken cancellationToken = cancellationTokenSource.Token; var cancelCommand = isCancellable ? new CancelCommand(cancellationTokenSource) : null; var viewModel = new ProgressDialogViewModel( options, cancellationToken, cancelCommand); var window = new ProgressDialog { 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 <ProgressReport>, Task> action, ProgressDialogOptions options, bool isCancellable = true) { if (action == null) { throw new ArgumentNullException(nameof(action)); } if (options == null) { throw new ArgumentNullException(nameof(options)); } using (var cancellationTokenSource = new CancellationTokenSource()) { CancellationToken cancellationToken = cancellationTokenSource.Token; var cancelCommand = isCancellable ? new CancelCommand(cancellationTokenSource) : null; var viewModel = new ProgressDialogViewModel( options, cancellationToken, cancelCommand); var window = new ProgressDialog { DataContext = viewModel }; Task task = action(cancellationToken, viewModel.Progress); #pragma warning disable 4014 task.ContinueWith(_ => viewModel.Close = true); #pragma warning restore 4014 window.ShowDialog(); await task; } }
public async Task <T> ExecuteAsync <T>(Func <IProgress <ProgressReport>, Task <T> > action, ProgressDialogOptions options) { if (action == null) { throw new ArgumentNullException(nameof(action)); } return(await ExecuteAsyncInternal((token, progress) => action(progress), options, isCancellable : false)); }