/// <summary> /// Attempt to load the controller with the current settings, /// showing the settings dialog if can't load /// </summary> private void TryLoadController() { try { LoadController(); } catch (ControllerConnectionException cex) { Exception ex = cex; // Show the settings dialog if an exception occurrs, // to allow the user to re-configure if necessary. while (true) { var fm = new SettingsWindow(); var result = ExceptionDialog.ShowException("Could not connect to the device. Check application settings and try again", ex, ExceptionSeverity.Warning); // Ignore if (result == ExceptionDialog.ModalResult.Ignore) { break; // Just start the application } bool?_ok = fm.ShowDialog(); bool ok = (_ok.HasValue && _ok.Value); // Cancel, exit application if (!ok) { Shutdown(1); return; } // If settings have changed, we'll need to re-load the controller try { LoadController(); break; // Settings are now valid, don't loop } catch (Exception _ex) { ex = _ex; // For next iteration of the loop } } } }
/// <summary> /// Try and load all engines present /// </summary> private void LoadEngines(ControlWindow controlWindow) { var engineTypes = Engines.ListEngines(); // Load engines & populate tabs foreach (var engineType in engineTypes) { string name = Engines.GetEngineName(engineType); // Try to load the engine HexEngine engine = null; UserControl page = null; try { engine = Engines.LoadEngine(engineType); page = engine.GetControlPage(); } catch (NotImplementedException) { continue; } catch (Exception ex) { string assyName = Plugins.GetAssemblyName(engineType); if (assyName != null) { name += " (" + assyName + ")"; } ExceptionDialog.ShowException(String.Format("Could not load {0}", name), ex, ExceptionSeverity.Error); continue; // Ignore was pressed } // Add tab & page to the control window controlWindow.AddEngine(name, page, engine); } }
void Dispatcher_UnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e) { try { Exception outer_ex = e.Exception; Exception ex = e.Exception; // If exception occurred in another thread if (e.Exception is System.Reflection.TargetInvocationException && e.Exception.InnerException != null) { ex = e.Exception.InnerException; outer_ex = e.Exception.InnerException; } if (ex is TimerException) { ex = ex.InnerException; } ExceptionDialog.ShowException(null, ex, ExceptionSeverity.Unhandled); // Re-enable timer if Ignoring the exception if (outer_ex is TimerException && (outer_ex as TimerException).Timer.Enabled == false) { (outer_ex as TimerException).Timer.Start(); connectTimer.Start(); } e.Handled = true; } catch (Exception) { // Fail-safe Shutdown(1); return; } }
/// <summary> /// Show an exception dialog of varying severity: /// ExceptionSeverity.Warning /// Does not abort the program /// Buttons: [Ok] /// ExceptionSeverity.Error /// A (possibly expected) error that can be recovered from. /// Buttons: [Debug] [Ignore] [Abort] /// ExceptionSeverity.Unhandled /// An unexpected/unhandled exception, probably caught by the global exception handler function. /// The exception can still be ignored, but this may lead to abnormal program operation. /// Buttons: [Debug] [Ignore] [Abort] /// ExceptionSeverity.Critical /// An unrecoverable error - user can only abort the program /// Buttons: [Debug] [Abort] /// </summary> /// <param name="message">The message to present to the user</param> /// <param name="ex">The exception that caused this message to appear (optional)</param> /// <param name="severity">The severity of the exception</param> /// <returns>The button that was clicked</returns> public static ModalResult ShowException(string message, Exception ex = null, ExceptionSeverity severity = ExceptionSeverity.Error) { var fm = new ExceptionDialog(); fm.messageLabel.Text = (message != null) ? message : ""; if (ex != null) { if (message != null) { fm.messageLabel.Text += "\n\nDetail:\n"; } fm.messageLabel.Text += ex.Message; fm.detailsLabel.Text = "Exception Details:\n\n" + BuildExceptionDetails(ex); } else { fm.detailsGrid.Visibility = Visibility.Hidden; fm.btnShowDetails.Visibility = Visibility.Hidden; } fm.Title = "HexLight Controller"; switch (severity) { case ExceptionSeverity.Critical: fm.btnIgnore.Visibility = Visibility.Collapsed; fm.btnDebug.Visibility = Visibility.Collapsed; fm.btnOk.Visibility = Visibility.Collapsed; fm.btnAbort.IsCancel = true; fm.btnAbort.IsDefault = true; fm.Title += " - Critical Error"; break; case ExceptionSeverity.Unhandled: fm.btnOk.Visibility = Visibility.Collapsed; fm.btnAbort.IsCancel = true; fm.btnAbort.IsDefault = true; fm.Title += " - Unhandled Exception"; break; case ExceptionSeverity.Error: fm.btnOk.Visibility = Visibility.Collapsed; //fm.btnAbort.Visibility = Visibility.Collapsed; fm.btnIgnore.IsCancel = true; fm.btnIgnore.IsDefault = true; fm.Title += " - Error"; break; case ExceptionSeverity.Warning: fm.btnDebug.Visibility = Visibility.Collapsed; fm.btnAbort.Visibility = Visibility.Collapsed; fm.btnIgnore.Visibility = Visibility.Collapsed; fm.btnOk.IsCancel = true; fm.btnOk.IsDefault = true; fm.Title += " - Error"; break; } fm.ShowDialog(); // Shutdown the app if required if (severity == ExceptionSeverity.Critical || fm.modalResult == ModalResult.Abort) { Application.Current.Shutdown(1); System.Environment.Exit(1); return(fm.modalResult); } if (fm.modalResult == ModalResult.Debug) { Debugger.Launch(); Debugger.Break(); // Welcome, you have been brought here from the [Debug] button. // Where to from here? // - Try stepping out to see what caused the exception // - Examin the stack traces to see what caused the exception } return(fm.modalResult); }