From 3b893ebfc885b6fb7f33ac8efb4573e860bf2f47 Mon Sep 17 00:00:00 2001 From: Stefan Koell Date: Mon, 20 Jun 2022 12:48:44 +0200 Subject: [PATCH] Continue embedding the window when ProcessJobTracker fails ProcessJobTracker.AddProcess may fail but this shouldn't be a reason to cancel the whole window embedding. If it fails, a warning message is logged instead. --- .../ExternalApps.cs | 19 ++++++++++++++++--- ...pps.Community.ExternalApps.WinForms.csproj | 2 +- .../WindowManagement/ExternalApp.cs | 2 +- 3 files changed, 18 insertions(+), 5 deletions(-) diff --git a/src/RoyalApps.Community.ExternalApps.WinForms/ExternalApps.cs b/src/RoyalApps.Community.ExternalApps.WinForms/ExternalApps.cs index bb58a54..ab817d3 100644 --- a/src/RoyalApps.Community.ExternalApps.WinForms/ExternalApps.cs +++ b/src/RoyalApps.Community.ExternalApps.WinForms/ExternalApps.cs @@ -5,6 +5,7 @@ using Windows.Win32; using Windows.Win32.Foundation; using Windows.Win32.UI.WindowsAndMessaging; +using Microsoft.Extensions.Logging; using RoyalApps.Community.ExternalApps.WinForms.WindowManagement; namespace RoyalApps.Community.ExternalApps.WinForms; @@ -35,7 +36,7 @@ public static void Cleanup() ProcessJobTracker.Dispose(); } - internal static HWND EmbedWindow(HWND parentWindowHandle, HWND childWindowHandle, Process? process) + internal static HWND EmbedWindow(HWND parentWindowHandle, HWND childWindowHandle, Process? process, ILogger logger) { if (!_isInitialized) throw new InvalidOperationException("Cannot call EmbedWindow without calling 'ExternalApps.Initialize()' first."); @@ -49,8 +50,20 @@ internal static HWND EmbedWindow(HWND parentWindowHandle, HWND childWindowHandle parentWindowClientRect.right - parentWindowClientRect.left, parentWindowClientRect.bottom - parentWindowClientRect.top); - if (process != null) - ProcessJobTracker.AddProcess(process); + try + { + if (process != null) + ProcessJobTracker.AddProcess(process); + } + catch (Exception ex) + { + logger.LogWarning( + ex, + "ProcessJobTracker could not add the process {FileName} with the id {Id}", + process?.StartInfo.FileName, + process?.Id + ); + } return new HWND(containerHandle); } diff --git a/src/RoyalApps.Community.ExternalApps.WinForms/RoyalApps.Community.ExternalApps.WinForms.csproj b/src/RoyalApps.Community.ExternalApps.WinForms/RoyalApps.Community.ExternalApps.WinForms.csproj index b1f1beb..2b3dc23 100644 --- a/src/RoyalApps.Community.ExternalApps.WinForms/RoyalApps.Community.ExternalApps.WinForms.csproj +++ b/src/RoyalApps.Community.ExternalApps.WinForms/RoyalApps.Community.ExternalApps.WinForms.csproj @@ -11,7 +11,7 @@ README.md RoyalApps_1024.png MIT - 0.3.0 + 0.3.1 enable 10 true diff --git a/src/RoyalApps.Community.ExternalApps.WinForms/WindowManagement/ExternalApp.cs b/src/RoyalApps.Community.ExternalApps.WinForms/WindowManagement/ExternalApp.cs index c7037f3..c0846d9 100644 --- a/src/RoyalApps.Community.ExternalApps.WinForms/WindowManagement/ExternalApp.cs +++ b/src/RoyalApps.Community.ExternalApps.WinForms/WindowManagement/ExternalApp.cs @@ -488,7 +488,7 @@ public async Task EmbedAsync(ExternalAppHost ownerControl, CancellationToken can OriginalWindowHandle = WindowHandle; ownerControl.Invoke(() => { - WindowHandle = ExternalApps.EmbedWindow(ownerControl.ControlHandle, WindowHandle, Process); + WindowHandle = ExternalApps.EmbedWindow(ownerControl.ControlHandle, WindowHandle, Process, _logger); }); IsEmbedded = true; break;