Exemplo n.º 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);
            }
        }
Exemplo n.º 2
0
        internal 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) =>
            {
                try
                {
                    Current = new ProgressDialogContext(s as BackgroundWorker, e as DoWorkEventArgs);

                    if (operation is Action)
                    {
                        ((Action)operation)();
                    }
                    else if (operation is Func <object> )
                    {
                        e.Result = ((Func <object>)operation)();
                    }
                    else
                    {
                        throw new InvalidOperationException("Operation type is not supoorted");
                    }

                    // NOTE: Always do this check in order to avoid default processing after the Cancel button has been pressed.
                    // This call will set the Cancelled flag on the result structure.
                    Current.CheckCancellationPending();
                }
                catch (ProgressDialogCancellationExcpetion)
                { }
                catch (Exception ex)
                {
                    if (!Current.CheckCancellationPending())
                    {
                        throw ex;
                    }
                }
                finally
                {
                    Current = null;
                }
            };

            _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)
                {
                    SubLabel          = (e.UserState as string) ?? string.Empty;
                    ProgressBar.Value = e.ProgressPercentage;
                }
            };

            _worker.RunWorkerAsync();

            ShowDialog();

            return(result);
        }