Пример #1
0
        public static DialogResult Show <TObject>(
            IWin32Window owner,
            string caption,
            Func <object, object> asyncMethod,
            object asyncParameterIn,
            out bool asyncWasCanceled, out Exception asyncExceptionOrNull, out TObject asyncDoneParameter)
        {
            DialogResult result;

            if ((object)asyncMethod == null)
            {
                throw new ArgumentNullException("asyncMethod");
            }

            using (BackgroundTaskForm backgroundTaskForm = new BackgroundTaskForm())
            {
                backgroundTaskForm.btnCancel.Enabled = false;
                backgroundTaskForm.lblCaption.Text   = caption;
                backgroundTaskForm.AsyncMethod       = asyncMethod;
                backgroundTaskForm.AsyncParameterIn  = asyncParameterIn;

                result = backgroundTaskForm.ShowDialog(owner);

                asyncWasCanceled     = backgroundTaskForm.AsyncCanceledOut;
                asyncExceptionOrNull = backgroundTaskForm.AsyncErrorOut;
                asyncDoneParameter   = (TObject)backgroundTaskForm.AsyncResultOut;
            }

            return(result);
        }
Пример #2
0
        TObject IFullView.ShowAsync <TObject>(string text, Func <object, TObject> asyncCallback, object asyncParameter)
        {
            DialogResult dialogResult;
            TObject      asyncResult;
            bool         asyncWasCanceled;
            Exception    asyncExceptionOrNull;

            if ((object)asyncCallback == null)
            {
                throw new ArgumentNullException("asyncCallback");
            }

            this._.StatusText = "Asynchronous operation started...";

            dialogResult = BackgroundTaskForm.Show <TObject>(this, text, o =>
            {
                //Thread.Sleep(1000);
                return(asyncCallback(asyncParameter));
            }, null, out asyncWasCanceled, out asyncExceptionOrNull, out asyncResult);

            if (asyncWasCanceled || dialogResult == DialogResult.Cancel)
            {
                this.Close();                 // direct
            }
            if ((object)asyncExceptionOrNull != null)
            {
                if (ExecutableApplicationFascade.Current.HookUnhandledExceptionEvents)
                {
                    ExecutableApplicationFascade.Current.ShowNestedExceptionsAndThrowBrickAtProcess(asyncExceptionOrNull);
                }
                // should never reach this point
            }

            this._.StatusText = "Asynchronous operation completed successfully.";

            return(asyncResult);
        }