Ejemplo n.º 1
0
        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();
            }
        }
Ejemplo n.º 2
0
 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));
 }
Ejemplo n.º 3
0
 public void Execute(Action <IProgress <string> > action, ProgressDialogOptions options)
 {
     if (action == null)
     {
         throw new ArgumentNullException("action");
     }
     ExecuteInternal((token, progress) => action(progress), options);
 }
Ejemplo n.º 4
0
 public void Execute(Action <CancellationToken> action, ProgressDialogOptions options)
 {
     if (action == null)
     {
         throw new ArgumentNullException("action");
     }
     ExecuteInternal((token, progress) => action(token), options);
 }
Ejemplo n.º 5
0
 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));
 }
Ejemplo n.º 6
0
 public async Task ExecuteAsync(Func <CancellationToken, Task> action, ProgressDialogOptions options)
 {
     if (action == null)
     {
         throw new ArgumentNullException("action");
     }
     await ExecuteAsyncInternal((token, progress) => action(token), options);
 }
Ejemplo n.º 7
0
 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));
 }
Ejemplo n.º 8
0
 public void Execute(
     Action <CancellationToken, IProgress <string> > action, ProgressDialogOptions options)
 {
     if (action == null)
     {
         throw new ArgumentNullException("action");
     }
     ExecuteInternal(action, options);
 }
Ejemplo n.º 9
0
 public void Execute(Action action, ProgressDialogOptions options)
 {
     if (action == null)
     {
         throw new ArgumentNullException("action");
     }
     ExecuteInternal((token, progress) => action(), options,
                     isCancellable: false);
 }
Ejemplo n.º 10
0
 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));
 }
Ejemplo n.º 11
0
 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);
 }
Ejemplo n.º 12
0
        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);
            }
        }
Ejemplo n.º 13
0
 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);
 }
Ejemplo n.º 14
0
        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;
            }
        }