private static bool pleaseWaitImplementation(bool canBeCanceled, int?progressMax, MyRunMethod method) { bool res = true; Debug.WriteLine("PleaseWait.Run start"); //Cancel = false; // Start my lengthy Process // Display the Please Wait Dialog here PleaseWaitWindow _pleaseWaitDlg = new PleaseWaitWindow(canBeCanceled, progressMax); Thread lengthyProcessThread = new Thread(threadRunner); 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); Debug.WriteLine("Creating thread: " + lengthyProcessThread.ManagedThreadId); _pleaseWaitDlg.ShowDialog(); //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); }
private static void threadRunner(object obj) { ControlHelper.EnsureThreadLocalized(); PleaseWaitContext context = (PleaseWaitContext)obj; PleaseWaitWindow temporaryWindow = (PleaseWaitWindow)context.ProgressWindow; MethodParameters par = new MethodParameters(temporaryWindow); temporaryWindow.Parameters = par; try { Debug.WriteLine("PleaseWait.Invoke before"); context.Method.Invoke(par); Debug.WriteLine("PleaseWait.Invoke after"); } catch (Exception ex) { context.ReturnException = ex; } temporaryWindow.ThreadSafeClose(); Debug.WriteLine("PleaseWait.Run end"); }