From f15f8919c6b9551654ea4d9b922adf992872a73e Mon Sep 17 00:00:00 2001 From: pinzart90 Date: Fri, 2 Feb 2024 09:51:30 -0500 Subject: [PATCH] Update DynamoViewModel.cs --- .../ViewModels/Core/DynamoViewModel.cs | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/src/DynamoCoreWpf/ViewModels/Core/DynamoViewModel.cs b/src/DynamoCoreWpf/ViewModels/Core/DynamoViewModel.cs index 7d4074144da..9a8754cb0cf 100644 --- a/src/DynamoCoreWpf/ViewModels/Core/DynamoViewModel.cs +++ b/src/DynamoCoreWpf/ViewModels/Core/DynamoViewModel.cs @@ -795,8 +795,9 @@ private void CurrentDispatcher_UnhandledException(object sender, DispatcherUnhan return; } - // Try to handle the exception so that the host app can continue in most cases. - // In some cases Dynamo code might still crash after this handler kicks in. In these edge cases we might see 2 CER windows (the extra one from the host app) + // Try to handle the exception so that the host app can continue (in most cases). + // In some cases Dynamo code might still crash after this handler kicks in. In these edge cases + // we might see 2 CER windows (the extra one from the host app) - CER tool might handle this in the future. e.Handled = true; CrashGracefully(e.Exception); @@ -822,6 +823,9 @@ internal void CrashGracefully(Exception ex, bool fatal = false) if (fatal) { + // Fatal exception. Close Dynamo and terminate the process. + + // Run the Dynamo exit code in the UI thread since CrashGracefully could be called in other threads too. UIDispatcher?.Invoke(() => { try { @@ -829,11 +833,17 @@ internal void CrashGracefully(Exception ex, bool fatal = false) } catch { } }, DispatcherPriority.Send); - + + // We terminate the process here so that no other exceptions can leak to the host apps. + // All fatal exceptions will result in a process termination (either by Dynamo, host app or CLR) Environment.Exit(1); } else { + // Non fatal exception. + + // We run the Dynamo exit call asyncronously in the dispatcher to ensure that any continuation of code + // manages to run to completion before we start shutting down Dynamo. UIDispatcher?.BeginInvoke(() => { try {