private void FormProgressStatus_Shown(object sender, EventArgs e) { //Spawn a separate thread that will monitor if this progress form should has indicated that it needs to close. //This thread will be a fail-safe in the sense that it will constantly monitor a separate indicator that this window should close. ODThread threadForceCloseMonitor = new ODThread(100, new ODThread.WorkerDelegate((o) => { if (_hasClosed) { o.QuitAsync(); //Stop monitoring as soon as we detect that this window has been "closed". return; } if (ForceClose) { //Something triggered the fact that this form should have closed. this.InvokeIfRequired(() => { DialogResult = DialogResult.OK; ODException.SwallowAnyException(() => Close()); //Can throw exceptions for many reasons. }); o.QuitAsync(); //We tried our best to "unstuck" this window. The user will have to Alt + F4 this window closed? return; } })); threadForceCloseMonitor.AddExceptionHandler((ex) => { ex.DoNothing(); //The form might stay open forever which was already happening... we tried our best. }); threadForceCloseMonitor.Name = "FormProgressStatusMonitor_" + DateTime.Now.Ticks; threadForceCloseMonitor.Start(); }
public FormProgressBase(ODEventType odEventType, Type eventType) { this.FormClosing += new FormClosingEventHandler(this.FormProgressStatus_FormClosing); this.FormClosed += new FormClosedEventHandler(this.FormProgressStatus_FormClosed); this.Shown += new EventHandler(this.FormProgressStatus_Shown); if (eventType == null) { eventType = typeof(ODEvent); } _eventType = eventType; _odEventType = odEventType; //Registers this form for any progress status updates that happen throughout the entire program. ODException.SwallowAnyException(() => _eventInfoFired = _eventType.GetEvent("Fired")); if (_eventInfoFired == null) { throw new ApplicationException("The 'eventType' passed into FormProgressStatus does not have a 'Fired' event.\r\n" + "Type passed in: " + eventType.GetType()); } //Register the "Fired" event to the ODEvent_Fired delegate. Delegate delegateFired = Delegate.CreateDelegate(_eventInfoFired.EventHandlerType, this, "ODEvent_Fired"); MethodInfo methodAddHandler = _eventInfoFired.GetAddMethod(); methodAddHandler.Invoke(this, new object[] { delegateFired }); }