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