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();
            }
        }
 public bool TryExecute <T>(Func <CancellationToken, T> action, ProgressDialogOptions options, out T result)
 {
     if (action == null)
     {
         throw new ArgumentNullException("action");
     }
     return(TryExecuteInternal((token, progress) => action(token), options, out result));
 }
 public void Execute(Action <IProgress <string> > action, ProgressDialogOptions options)
 {
     if (action == null)
     {
         throw new ArgumentNullException("action");
     }
     ExecuteInternal((token, progress) => action(progress), options);
 }
 public void Execute(Action <CancellationToken> action, ProgressDialogOptions options)
 {
     if (action == null)
     {
         throw new ArgumentNullException("action");
     }
     ExecuteInternal((token, progress) => action(token), options);
 }
 public async Task <T> ExecuteAsync <T>(Func <Task <T> > action, ProgressDialogOptions options)
 {
     if (action == null)
     {
         throw new ArgumentNullException("action");
     }
     return(await ExecuteAsyncInternal((token, progress) => action(), options));
 }
 public async Task ExecuteAsync(Func <CancellationToken, Task> action, ProgressDialogOptions options)
 {
     if (action == null)
     {
         throw new ArgumentNullException("action");
     }
     await ExecuteAsyncInternal((token, progress) => action(token), options);
 }
 public bool TryExecute <T>(Func <IProgress <string>, T> action, ProgressDialogOptions options, out T result)
 {
     if (action == null)
     {
         throw new ArgumentNullException("action");
     }
     return(TryExecuteInternal((token, progress) => action(progress), options, out result,
                               isCancellable: false));
 }
 public void Execute(
     Action <CancellationToken, IProgress <string> > action, ProgressDialogOptions options)
 {
     if (action == null)
     {
         throw new ArgumentNullException("action");
     }
     ExecuteInternal(action, options);
 }
 public void Execute(Action action, ProgressDialogOptions options)
 {
     if (action == null)
     {
         throw new ArgumentNullException("action");
     }
     ExecuteInternal((token, progress) => action(), options,
                     isCancellable: false);
 }
 public async Task <T> ExecuteAsync <T>(Func <CancellationToken, IProgress <string>, Task <T> > action,
                                        ProgressDialogOptions options)
 {
     if (action == null)
     {
         throw new ArgumentNullException("action");
     }
     return(await ExecuteAsyncInternal(action, options));
 }
 public async Task ExecuteAsync(Func <IProgress <string>, Task> action, ProgressDialogOptions options)
 {
     if (action == null)
     {
         throw new ArgumentNullException("action");
     }
     await ExecuteAsyncInternal((token, progress) => action(progress), options,
                                isCancellable : false);
 }
 public ProgressDialogWindowViewModel(
     ProgressDialogOptions options,
     CancellationToken cancellationToken,
     CancelCommand cancelCommand)
 {
     if (options == null) throw new ArgumentNullException("options");
     WindowTitle = options.WindowTitle;
     Label = options.Label;
     CancelCommand = cancelCommand; // can be null (not cancellable)
     cancellationToken.Register(OnCancelled);
     Progress = new Progress<string>(OnProgress);
 }
        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);
            }
        }
 public ProgressDialogWindowViewModel(
     ProgressDialogOptions options,
     CancellationToken cancellationToken,
     CancelCommand cancelCommand)
 {
     if (options == null)
     {
         throw new ArgumentNullException("options");
     }
     WindowTitle   = options.WindowTitle;
     Label         = options.Label;
     CancelCommand = cancelCommand; // can be null (not cancellable)
     cancellationToken.Register(OnCancelled);
     Progress = new Progress <string>(OnProgress);
 }
        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;
            }
        }