using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Nona; namespace Foundations { public class GuiExceptionHandler : IExceptionHandler { public static string RenderCrashMessage = "The render thread has crashed. The simulation is unaffected by this and should still be running in " + "the background. However, the graphics won't update anymore until you manually re-enable the render " + "thread"; public static string SimulationCrashMessage = "The simulation thread has crashed, and is therefore currently paused. If you continue, the " + "simulation will remain in a paused state until you unpause it."; public static string DrawSceneCrashMessage = "The DrawScene callback has chrashed, and has therefore been paused. The simulation is unaffected by " + "this and should still be running in the background. However, the graphics won't update anymore until " + "you manually re-enable the draw scene callback."; public static string UiCrashMessage = "The UI pump thread has crashed. We can attempt to continue, though. The simulation thread should be " + "unaffected."; public static string UnknownCrashMessage = "Unhandled exception on unknown thread. This is (probably) not recoverable."; private static bool CreateExceptionDialog(Exception exc, string furtherInformation, bool startNewMessagePump) { var excDialog = new Lima.ExceptionDialog(); excDialog.FurtherInformation = furtherInformation; excDialog.Exception = exc; excDialog.Icon = Properties.Resources.bug; Lima.ExceptionDialog.RunExceptionDialog(excDialog, startNewMessagePump); return true; } private static ProtectedLoop.TryHandleExceptionDelegate CreateExceptionHandler( string furtherInformation, bool startNewMessagePump = true) { return exception => { CreateExceptionDialog(exception, furtherInformation, startNewMessagePump); return true; }; } public void ThreadExceptionHandler(object sender, System.Threading.ThreadExceptionEventArgs exception) { CreateExceptionDialog(exception.Exception, UiCrashMessage, startNewMessagePump: false); } public void UnknownExceptionHandler(object sender, UnhandledExceptionEventArgs exception) { // Non UI bug in some other thread. System.Windows.Forms.MessageBox.Show(UnknownCrashMessage + "Details: " + Environment.NewLine + exception.ExceptionObject.ToString(), "Unknown error on unknown thread."); } public bool SimulationExceptionHandler(Exception exception) { return CreateExceptionDialog(exception, SimulationCrashMessage, startNewMessagePump: true); } public bool RenderExceptionHandler(Exception exception) { return CreateExceptionDialog(exception, RenderCrashMessage, startNewMessagePump: true); } public bool DrawExceptionHandler(Exception exception) { return CreateExceptionDialog(exception, DrawSceneCrashMessage, startNewMessagePump: false); } } }