From 84b79f0f9b46469654e9ebe2cd911ba2bcbdc5a6 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sat, 28 May 2022 00:49:14 +0200 Subject: [PATCH] Improve WinUI 3 sample app (#295) * Fix WinUI sample app crash when closing * Tweak WinUI 3 sample project files Also disables ReadyToRun for Debug builds --- .../ComputeSharp.SwapChain.WinUI/App.xaml.cs | 27 ++++++++++++++++--- .../ComputeSharp.SwapChain.WinUI.csproj | 13 +++++---- .../PublishProfiles/win10-arm64.pubxml | 15 +++++------ .../PublishProfiles/win10-x64.pubxml | 13 +++++---- .../Views/MainWindow.xaml | 2 +- .../Views/MainWindow.xaml.cs | 26 ++++++++++++++++++ 6 files changed, 69 insertions(+), 27 deletions(-) diff --git a/samples/ComputeSharp.SwapChain.WinUI/App.xaml.cs b/samples/ComputeSharp.SwapChain.WinUI/App.xaml.cs index 277cdff4..6068c97f 100644 --- a/samples/ComputeSharp.SwapChain.WinUI/App.xaml.cs +++ b/samples/ComputeSharp.SwapChain.WinUI/App.xaml.cs @@ -1,4 +1,6 @@ -using ComputeSharp.SwapChain.WinUI.Views; +using System; +using System.Threading; +using ComputeSharp.SwapChain.WinUI.Views; using Microsoft.UI.Xaml; namespace ComputeSharp.SwapChain.WinUI; @@ -6,12 +8,18 @@ namespace ComputeSharp.SwapChain.WinUI; /// /// Provides application-specific behavior to supplement the default Application class. /// -public partial class App : Application +public sealed partial class App : Application { /// /// The main application window /// - private Window? mainWindow; + private MainWindow? mainWindow; + + /// + /// A timer to force shutdown after a given amount of time. + /// + /// This field is needed to keep the timer alive. + private Timer? shutdownTimer; /// /// Initializes the singleton application object. This is the first line of authored code @@ -26,7 +34,18 @@ public partial class App : Application protected override void OnLaunched(LaunchActivatedEventArgs args) { mainWindow = new MainWindow(); - mainWindow.Closed += (_, _) => Exit(); + mainWindow.Closed += (_, _) => + { + // Due to an issue (https://discord.com/channels/372137812037730304/663434534087426129/979543152853663744), the WinUI 3 app + // will keep running after the window is closed if any render thread is still going. To work around this, each loaded shader + // panel is tracked in a conditional weak table, and then when the window is closed they're all manually stopped by setting + // their shader runners to null. After this, the app should just be able to close automatically when the panels are done. + mainWindow.OnShutdown(); + + // For good measure, also start a timer of one second. If the app is still alive by then (ie. if the timer callback is + // executed at all), then just force the whole process to terminate, and exit with the E_APPLICATION_EXITING error code. + shutdownTimer = new Timer(_ => Environment.Exit(unchecked((int)(0x8000001A))), this, 1000, Timeout.Infinite); + }; mainWindow.Activate(); } } diff --git a/samples/ComputeSharp.SwapChain.WinUI/ComputeSharp.SwapChain.WinUI.csproj b/samples/ComputeSharp.SwapChain.WinUI/ComputeSharp.SwapChain.WinUI.csproj index f62c81b7..454356e5 100644 --- a/samples/ComputeSharp.SwapChain.WinUI/ComputeSharp.SwapChain.WinUI.csproj +++ b/samples/ComputeSharp.SwapChain.WinUI/ComputeSharp.SwapChain.WinUI.csproj @@ -5,7 +5,7 @@ 10.0.17763.0 ComputeSharp.SwapChain.WinUI app.manifest - x64;ARM64 + x64;arm64 win10-x64;win10-arm64 win10-$(Platform).pubxml true @@ -13,11 +13,6 @@ $(DefineConstants);SAMPLE_APP - - - Designer - - @@ -38,6 +33,11 @@ + + + + + @@ -46,6 +46,5 @@ - diff --git a/samples/ComputeSharp.SwapChain.WinUI/Properties/PublishProfiles/win10-arm64.pubxml b/samples/ComputeSharp.SwapChain.WinUI/Properties/PublishProfiles/win10-arm64.pubxml index 318e1b94..f1f86f87 100644 --- a/samples/ComputeSharp.SwapChain.WinUI/Properties/PublishProfiles/win10-arm64.pubxml +++ b/samples/ComputeSharp.SwapChain.WinUI/Properties/PublishProfiles/win10-arm64.pubxml @@ -1,19 +1,18 @@  - + FileSystem - ARM64 + arm64 win10-arm64 bin\$(Configuration)\$(TargetFramework)\$(RuntimeIdentifier)\publish\ true False - True - \ No newline at end of file diff --git a/samples/ComputeSharp.SwapChain.WinUI/Properties/PublishProfiles/win10-x64.pubxml b/samples/ComputeSharp.SwapChain.WinUI/Properties/PublishProfiles/win10-x64.pubxml index 78bdd7d0..58a18b9a 100644 --- a/samples/ComputeSharp.SwapChain.WinUI/Properties/PublishProfiles/win10-x64.pubxml +++ b/samples/ComputeSharp.SwapChain.WinUI/Properties/PublishProfiles/win10-x64.pubxml @@ -1,7 +1,5 @@  - + FileSystem @@ -10,10 +8,11 @@ https://go.microsoft.com/fwlink/?LinkID=208121. bin\$(Configuration)\$(TargetFramework)\$(RuntimeIdentifier)\publish\ true False - True - \ No newline at end of file diff --git a/samples/ComputeSharp.SwapChain.WinUI/Views/MainWindow.xaml b/samples/ComputeSharp.SwapChain.WinUI/Views/MainWindow.xaml index 3fb1bb54..9f48ba45 100644 --- a/samples/ComputeSharp.SwapChain.WinUI/Views/MainWindow.xaml +++ b/samples/ComputeSharp.SwapChain.WinUI/Views/MainWindow.xaml @@ -119,7 +119,7 @@ - +