コード例 #1
0
        private static void threadRunner(object obj)
        {
            Helper.EnsureThreadLocalized();
            PleaseWaitContext context = (PleaseWaitContext)obj;


            try
            {
                Debug.WriteLine("PleaseWait.Invoke before");
                context.Method.Invoke(context.ProgressWindow.Parameters);
                Debug.WriteLine("PleaseWait.Invoke after");
            }
            catch (Exception ex)
            {
                context.ReturnException = ex;
                //throw;
            }
            finally
            {
                //timer.Stop();
                context.ProgressWindow.Parameters.Finished = true;
                context.ProgressWindow.ThreadSafeClose();
                Debug.WriteLine("PleaseWait.Run end");
            }
        }
コード例 #2
0
        //static DispatcherTimer timer = new DispatcherTimer();
        private static bool pleaseWaitImplementation(bool canBeCanceled, int?progressMax, MyRunMethod method, IProgressWindow _pleaseWaitDlg)
        {
            bool res = true;

            Debug.WriteLine("PleaseWait.Run start");
            //Cancel = false;
            // Start my lengthy Process
            // Display the Please Wait Dialog here
            if (_pleaseWaitDlg == null)
            {
                //_pleaseWaitDlg = new PleaseWaitWindow(canBeCanceled, progressMax);
                _pleaseWaitDlg = MainWindow.Instance;
            }

            Thread lengthyProcessThread = new Thread(threadRunner);

            lengthyProcessThread.SetApartmentState(ApartmentState.STA);
            lengthyProcessThread.IsBackground = true; // so not to have stray running threads if the main form is closed
            PleaseWaitContext pleaseWaitContext = new PleaseWaitContext(method, _pleaseWaitDlg);

            lengthyProcessThread.Start(pleaseWaitContext);

            IProgressWindow  temporaryWindow = _pleaseWaitDlg;
            MethodParameters par             = new MethodParameters(temporaryWindow);

            //timer.Interval = TimeSpan.FromSeconds(.5);
            //timer.Tick += delegate
            //{
            //    timer.Stop();
            //    if (/*!pendingClosing &&*/ !par.Finished)
            //    {
            //        temporaryWindow.ShowProgress();
            //    }
            //};
            temporaryWindow.Parameters = par;
            //timer.Start();
            temporaryWindow.ShowProgress(canBeCanceled, progressMax);


            Debug.WriteLine("Creating thread: " + lengthyProcessThread.ManagedThreadId);
            //if please wait thread throws exception then rethrow it
            if (pleaseWaitContext.ReturnException != null)
            {
                res = false;
                ExceptionHandler.Default.Process(pleaseWaitContext.ReturnException);
                //throw new RethrowedException(pleaseWaitContext.ReturnException);)
                throw pleaseWaitContext.ReturnException;
            }
            return(res);
        }