Exemplo 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();
            }
        }
Exemplo 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));
 }
Exemplo 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);
 }
Exemplo n.º 4
0
 public void Execute(Action <CancellationToken> action, ProgressDialogOptions options)
 {
     if (action == null)
     {
         throw new ArgumentNullException("action");
     }
     ExecuteInternal((token, progress) => action(token), options);
 }
Exemplo 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));
 }
Exemplo 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);
 }
Exemplo 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));
 }
Exemplo n.º 8
0
 public void Execute(
     Action <CancellationToken, IProgress <string> > action, ProgressDialogOptions options)
 {
     if (action == null)
     {
         throw new ArgumentNullException("action");
     }
     ExecuteInternal(action, options);
 }
Exemplo n.º 9
0
 public void Execute(Action action, ProgressDialogOptions options)
 {
     if (action == null)
     {
         throw new ArgumentNullException("action");
     }
     ExecuteInternal((token, progress) => action(), options,
                     isCancellable: false);
 }
Exemplo 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));
 }
Exemplo 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);
 }
Exemplo 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);
            }
        }
Exemplo 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);
 }
Exemplo 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;
            }
        }