Interaction logic for ProgressDialog.xaml
상속: Window
예제 #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();
            }
        }
예제 #2
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);
            }
        }
예제 #3
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;
            }
        }