From 3ad32e064d09c095c37973d2a31ec367ff52c8a0 Mon Sep 17 00:00:00 2001 From: "Aaron (Qilong)" Date: Wed, 22 May 2024 16:13:22 -0400 Subject: [PATCH 1/2] Add crash protection for window Owner setting --- .../UI/Prompts/DynamoMessageBox.xaml.cs | 44 ++++++++++--------- 1 file changed, 23 insertions(+), 21 deletions(-) diff --git a/src/DynamoCoreWpf/UI/Prompts/DynamoMessageBox.xaml.cs b/src/DynamoCoreWpf/UI/Prompts/DynamoMessageBox.xaml.cs index 0b20ba72e4e..5a49c8b8cf4 100644 --- a/src/DynamoCoreWpf/UI/Prompts/DynamoMessageBox.xaml.cs +++ b/src/DynamoCoreWpf/UI/Prompts/DynamoMessageBox.xaml.cs @@ -199,11 +199,7 @@ public static MessageBoxResult Show(Window owner, string messageBoxText, string MessageBoxButton = button, MessageBoxImage = icon }; - if (owner != null && owner.IsLoaded) - { - dynamoMessageBox.Owner = owner; - } - + SetOwnerWindow(owner, dynamoMessageBox); if (showRichTextBox) { dynamoMessageBox.BodyTextBlock.Visibility = Visibility.Collapsed; @@ -233,15 +229,30 @@ public static MessageBoxResult Show(Window owner,string messageBoxText, string c MessageBoxButton = button, MessageBoxImage = icon }; + SetOwnerWindow(owner, dynamoMessageBox); + dynamoMessageBox.ConfigureButtons(button); + dynamoMessageBox.ShowDialog(); + return dynamoMessageBox.CustomDialogResult; + } + /// + /// Set the owner window of the message box and prevent any exceptions that may occur + /// + /// Owner Window + /// New message box + internal static void SetOwnerWindow(Window owner, DynamoMessageBox dynamoMessageBox) + { if (owner != null && owner.IsLoaded) { - dynamoMessageBox.Owner = owner; + try + { + dynamoMessageBox.Owner = owner; + } + catch (Exception) + { + // In this case, we will not set the owner window + } } - - dynamoMessageBox.ConfigureButtons(button); - dynamoMessageBox.ShowDialog(); - return dynamoMessageBox.CustomDialogResult; } /// @@ -264,11 +275,7 @@ public static MessageBoxResult Show(Window owner, string messageBoxText, string MessageBoxButton = button, MessageBoxImage = icon }; - if (owner != null && owner.IsLoaded) - { - dynamoMessageBox.Owner = owner; - } - + SetOwnerWindow(owner, dynamoMessageBox); if (flags.TryGetValue(DialogFlags.Scrollable, out bool scrollable) && scrollable) { dynamoMessageBox.BodyTextBlock.Visibility = Visibility.Collapsed; @@ -361,12 +368,7 @@ internal static MessageBoxResult Show(Window owner, string messageBoxText, strin MessageBoxButton = button, MessageBoxImage = icon }; - - if (owner != null && owner.IsLoaded) - { - dynamoMessageBox.Owner = owner; - } - + SetOwnerWindow(owner, dynamoMessageBox); dynamoMessageBox.ConfigureButtons(button, buttonNames); dynamoMessageBox.ShowDialog(); return dynamoMessageBox.CustomDialogResult; From 09e9bfe6963b7d13987fa10255b2e7de25eb4e1c Mon Sep 17 00:00:00 2001 From: "Aaron (Qilong)" Date: Wed, 22 May 2024 16:21:23 -0400 Subject: [PATCH 2/2] Update --- .../UI/Prompts/DynamoMessageBox.xaml.cs | 40 +++++++++---------- 1 file changed, 20 insertions(+), 20 deletions(-) diff --git a/src/DynamoCoreWpf/UI/Prompts/DynamoMessageBox.xaml.cs b/src/DynamoCoreWpf/UI/Prompts/DynamoMessageBox.xaml.cs index 5a49c8b8cf4..08e13071e6e 100644 --- a/src/DynamoCoreWpf/UI/Prompts/DynamoMessageBox.xaml.cs +++ b/src/DynamoCoreWpf/UI/Prompts/DynamoMessageBox.xaml.cs @@ -235,26 +235,6 @@ public static MessageBoxResult Show(Window owner,string messageBoxText, string c return dynamoMessageBox.CustomDialogResult; } - /// - /// Set the owner window of the message box and prevent any exceptions that may occur - /// - /// Owner Window - /// New message box - internal static void SetOwnerWindow(Window owner, DynamoMessageBox dynamoMessageBox) - { - if (owner != null && owner.IsLoaded) - { - try - { - dynamoMessageBox.Owner = owner; - } - catch (Exception) - { - // In this case, we will not set the owner window - } - } - } - /// /// Displays a dialog to the user and returns their choice as a MessageBoxResult. /// @@ -374,6 +354,26 @@ internal static MessageBoxResult Show(Window owner, string messageBoxText, strin return dynamoMessageBox.CustomDialogResult; } + /// + /// Set the owner window of the message box and prevent any exceptions that may occur + /// + /// Owner Window + /// New message box + internal static void SetOwnerWindow(Window owner, DynamoMessageBox dynamoMessageBox) + { + if (owner != null && owner.IsLoaded) + { + try + { + dynamoMessageBox.Owner = owner; + } + catch (Exception) + { + // In this case, we will not set the owner window + } + } + } + public event PropertyChangedEventHandler PropertyChanged; ///