Skip to content

Commit

Permalink
Continue embedding the window when ProcessJobTracker fails
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
StefanKoell committed Jun 20, 2022
1 parent 76ddb27 commit 3b893eb
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 5 deletions.
19 changes: 16 additions & 3 deletions src/RoyalApps.Community.ExternalApps.WinForms/ExternalApps.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.");
Expand All @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<PackageReadmeFile>README.md</PackageReadmeFile>
<PackageIcon>RoyalApps_1024.png</PackageIcon>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<Version>0.3.0</Version>
<Version>0.3.1</Version>
<Nullable>enable</Nullable>
<LangVersion>10</LangVersion>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 3b893eb

Please sign in to comment.