コード例 #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();
            }
        }
コード例 #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);
 }
コード例 #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));
 }
コード例 #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);
 }
コード例 #5
0
 public void Execute(Action <CancellationToken> action, ProgressDialogOptions options)
 {
     if (action == null)
     {
         throw new ArgumentNullException(nameof(action));
     }
     ExecuteInternal((token, progress) => action(token), options);
 }
コード例 #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));
 }
コード例 #7
0
 public void Execute(
     Action <CancellationToken, IProgress <ProgressReport> > action, ProgressDialogOptions options)
 {
     if (action == null)
     {
         throw new ArgumentNullException(nameof(action));
     }
     ExecuteInternal(action, options);
 }
コード例 #8
0
 public void Execute(Action action, ProgressDialogOptions options)
 {
     if (action == null)
     {
         throw new ArgumentNullException(nameof(action));
     }
     ExecuteInternal((token, progress) => action(), options,
                     isCancellable: false);
 }
コード例 #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));
 }
コード例 #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);
            }
        }
コード例 #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;
            }
        }
コード例 #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));
 }