コード例 #1
0
        public static void Execute(Window owner, string label, Action operation, Action <ProgressDialogResult> successOperation, Action <ProgressDialogResult> failureOperation = null, Action <ProgressDialogResult> cancelledOperation = null)
        {
            ProgressDialogResult result = ExecuteInternal(owner, label, operation, null);

            if (result.Cancelled && cancelledOperation != null)
            {
                cancelledOperation(result);
            }
            else if (result.OperationFailed && failureOperation != null)
            {
                failureOperation(result);
            }
            else if (successOperation != null)
            {
                successOperation(result);
            }
        }
コード例 #2
0
        public ProgressDialogResult Execute(object operation)
        {
            if (operation == null)
            {
                throw new ArgumentNullException("operation");
            }

            ProgressDialogResult result = null;

            _isBusy = true;

            _worker = new BackgroundWorker();
            _worker.WorkerReportsProgress      = true;
            _worker.WorkerSupportsCancellation = true;

            _worker.DoWork +=
                (s, e) => {
                if (operation is Action)
                {
                    ((Action)operation)();
                }
                else if (operation is Action <BackgroundWorker> )
                {
                    ((Action <BackgroundWorker>)operation)(s as BackgroundWorker);
                }
                else if (operation is Action <BackgroundWorker, DoWorkEventArgs> )
                {
                    ((Action <BackgroundWorker, DoWorkEventArgs>)operation)(s as BackgroundWorker, e);
                }
                else if (operation is Func <object> )
                {
                    e.Result = ((Func <object>)operation)();
                }
                else if (operation is Func <BackgroundWorker, object> )
                {
                    e.Result = ((Func <BackgroundWorker, object>)operation)(s as BackgroundWorker);
                }
                else if (operation is Func <BackgroundWorker, DoWorkEventArgs, object> )
                {
                    e.Result = ((Func <BackgroundWorker, DoWorkEventArgs, object>)operation)(s as BackgroundWorker, e);
                }
                else
                {
                    throw new InvalidOperationException("Operation type is not supoorted");
                }
            };

            _worker.RunWorkerCompleted +=
                (s, e) => {
                result = new ProgressDialogResult(e);
                Dispatcher.BeginInvoke(DispatcherPriority.Send, (SendOrPostCallback) delegate {
                    _isBusy = false;
                    Close();
                }, null);
            };

            _worker.ProgressChanged +=
                (s, e) => {
                if (_worker.CancellationPending)
                {
                    return;
                }
                SubLabel          = (e.UserState as string) ?? string.Empty;
                ProgressBar.Value = e.ProgressPercentage;
            };

            _worker.RunWorkerAsync();

            ShowDialog();

            return(result);
        }