コード例 #1
0
        void ProgressDialogTestWithCancelButton()
        {
            // Easy way to pass data to the async method
            int millisecondsTimeout = 1500;

            ProgressDialogResult result = ProgressDialog.Execute(this, "Loading data", (bw, we) => {
                for (int i = 1; i <= 5; i++)
                {
                    if (ProgressDialog.ReportWithCancellationCheck(bw, we, "Executing step {0}/5...", i))
                    {
                        return;
                    }

                    Thread.Sleep(millisecondsTimeout);
                }

                // So 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.
                ProgressDialog.CheckForPendingCancellation(bw, we);
            }, ProgressDialogSettings.WithSubLabelAndCancel);

            if (result.Cancelled)
            {
                MessageBox.Show("ProgressDialog cancelled.");
            }
            else if (result.OperationFailed)
            {
                MessageBox.Show("ProgressDialog failed.");
            }
            else
            {
                MessageBox.Show("ProgressDialog successfully executed.");
            }
        }
コード例 #2
0
        void ProgressDialogTestDefault()
        {
            ProgressDialogResult result = ProgressDialog.Execute(this, "Loading data...", (bw, we) => {
                Thread.Sleep(4000);
            });

            if (result.OperationFailed)
            {
                MessageBox.Show("ProgressDialog failed.");
            }
            else
            {
                MessageBox.Show("ProgressDialog successfully executed.");
            }
        }
コード例 #3
0
        internal 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);
            }
        }
コード例 #4
0
        void ProgressDialogTestWithSubLabel()
        {
            ProgressDialogResult result = ProgressDialog.Execute(this, "Loading data", (bw) => {
                for (int i = 1; i <= 5; i++)
                {
                    ProgressDialog.Report(bw, "Executing step {0}/5...", i);

                    Thread.Sleep(1500);
                }
            }, ProgressDialogSettings.WithSubLabel);

            if (result.OperationFailed)
            {
                MessageBox.Show("ProgressDialog failed.");
            }
            else
            {
                MessageBox.Show("ProgressDialog successfully executed.");
            }
        }
コード例 #5
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
                {
                    ProgressDialog.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.
                    ProgressDialog.Current.CheckCancellationPending();
                }
                catch (ProgressDialogCancellationExcpetion)
                { }
                catch (Exception ex)
                {
                    if (!ProgressDialog.Current.CheckCancellationPending())
                    {
                        throw ex;
                    }
                }
                finally
                {
                    ProgressDialog.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);
        }
コード例 #6
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) =>
            {
                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)
                {
                    SubLabel          = (e.UserState as string) ?? string.Empty;
                    ProgressBar.Value = e.ProgressPercentage;
                }
            };

            _worker.RunWorkerAsync();

            //Modified: JSandusky => ShowDialog to Show
            Show();

            return(result);
        }
コード例 #7
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) =>
                {
                    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)
                    {
                        SubLabel = (e.UserState as string) ?? string.Empty;
                        ProgressBar.Value = e.ProgressPercentage;
                    }
                };

            _worker.RunWorkerAsync();

            //Modified: JSandusky => ShowDialog to Show
            Show();

            return result;
        }