///<summary>Closes out the form.</summary> public void Close() { if (_formProgress.InvokeRequired) { _formProgress.Invoke(() => Close()); //Not using BeginInvoke() because we want the form to be fully closed before proceeding return; } if (!_formProgress.IsDisposed) { if (_formProgress.IsHandleCreated) { _formProgress.Close(); } else { //ShowDialog() has not been called yet. _doClose = true; } } }
///<summary>Launches a progress window that will listen specifically for ODEvents with the passed in name. ///Returns an action that should be invoked whenever the long computations have finished which will close the progress window. ///eventName should be set to the name of the ODEvents that this specific progress window should be processing. ///eventType must be a Type that contains an event called "Fired" and a method called "Fire". ///Optionally set tag to an object that should be sent as the first "event arg" to the new progress window. /// This is typically a string that should be the very first message that shows to the user. /// If not set, the default message that will display will be "Please Wait..."</summary> public static Action ShowProgressStatus(string eventName, Type eventType, Form currentForm, bool hasHistory = false, bool hasMinimize = true, object tag = null) { bool isFormClosed = false; ODThread odThread = new ODThread(new ODThread.WorkerDelegate((ODThread o) => { FormProgressStatus FormPS = new FormProgressStatus(eventName, eventType, hasHistory, hasMinimize); FormPS.FormClosed += new FormClosedEventHandler((obj, e) => { isFormClosed = true; }); FormPS.TopMost = true; //Make this window show on top of ALL other windows. //Set the starting progress message if one was passed in. if (tag != null) { FormPS.ODEvent_Fired(new ODEventArgs(eventName, tag)); } //Check to make sure that the calling method hasn't already finished its long computations. //odThread's tag will be set to "true" if all computations have already finished and thus we do not need to show any progress window. if (o.Tag != null && o.Tag.GetType() == typeof(bool) && (bool)o.Tag) { try { FormPS.Close(); //Causes FormProgressStatus_FormClosing to get called which deregisters the ODEvent handler it currently has. } catch (Exception ex) { ex.DoNothing(); } return; } //From this point forward, the only way to kill FormProgressStatus is with a DEFCON 1 message via an ODEvent with the corresponding eventName. FormPS.ShowDialog(); })); odThread.SetApartmentState(ApartmentState.STA); //This is required for ReportComplex due to the history UI elements. odThread.AddExceptionHandler(new ODThread.ExceptionDelegate((Exception e) => { })); //Do nothing. odThread.Name = "ProgressStatusThread_" + eventName; odThread.Start(true); return(new Action(() => { //For progress threads, there is a race condition where the DEFCON 1 event will not get processed. //This is due to the fact that it took the thread longer to instantiate FormProgressStatus than it took the calling method to invoke this action. //Since we don't have access to FormProgressStatus within the thread from here, we simply flag odThread's Tag so that it knows to die. if (odThread != null) { odThread.Tag = true; } //Send the phrase that closes the window in case it is processing events. eventType.GetMethod("Fire").Invoke(eventType, new object[] { new ODEventArgs(eventName, "DEFCON 1") }); if (currentForm != null) { //When the form closed on the other thread it was sometimes causing the application to go behind other applications. Calling Activate() //brings the application back to the front or causes it to flash in the taskbar. DateTime start = DateTime.Now; while (!isFormClosed && (DateTime.Now - start).TotalSeconds < 1) //Wait till the form is closed or for one second { Thread.Sleep(1); } if (!currentForm.IsDisposed) { currentForm.Activate(); } } })); }