The exception is thrown and the process doesn't exit on window close
See original GitHub issueDescribe the bug
If we have Image and WebView2 control with Visibility=Collapsed at the same window, then, if we close the window, the window disappears, but the process doesn’t exit. If the debugger is attached, then the exception is observed.
Steps to reproduce the bug
-
Here is the Visual Studio solution: App1.zip
-
Create simple WinUI 3 project:
<!-- App.xaml -->
<Application
x:Class="App.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" />
// App.xaml.cs
using Microsoft.UI.Xaml;
namespace App;
public partial class App : Application
{
public App()
{
InitializeComponent();
}
protected override void OnLaunched(LaunchActivatedEventArgs args)
{
m_window = new MainWindow();
m_window.Activate();
}
private Window m_window;
}
<!-- MainWindow.xaml -->
<Window
x:Class="App.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<WebView2
Grid.Row="0"
Source="https://www.google.com"
Visibility="Collapsed" />
<Image
Grid.Row="1"
Height="30"
Source="https://www.google.com/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png" />
</Grid>
</Window>
// MainWindow.xaml.cs
using Microsoft.UI.Xaml;
namespace App;
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
}
- Launch the project and close the window.
- You’ll see in Task Manager that process still remains alive and doesn’t exit. If you have a debugger attached, you’ll see the following exception message:
Exception thrown at 0x00007FFD0FEA47F8 (threadpoolwinrt.dll) in App1.exe: 0xC0000005: Access violation reading location 0x0000000000000000.
- If you remove the
ImagefromMainWindow.xaml, then process exits and no exception is thrown. - If you have
WebView2.Visibility=Visible, then process exits and no exception is thrown.
Expected behavior
Process must exit. No exceptions should be thrown.
Screenshots
No response
NuGet package version
WinUI 3 - Windows App SDK 1.1.3
Windows app type
- UWP
- Win32
Device form factor
Desktop
Windows version
Windows 10 (21H2): Build 19044
Additional context
I observe this problem for both x86 and x64 platforms and for both Debug and Release configurations. 10.0.22621.1 Microsoft.Windows.SDK.BuildTools are used.
Issue Analytics
- State:
- Created a year ago
- Reactions:3
- Comments:6
Top Results From Across the Web
c# - Process.Close() is not working and ...
I have tried process.Close() but it didn't worked too(no exception was thrown). The process object shows System.InvalidOperationException.
Read more >Manage exceptions with the debugger in Visual Studio
The debugger can break execution at the point where an exception is thrown, so you may examine the exception before a handler is...
Read more >Exit step not throwing exception - webMethods
I've traced it, and I can see that the exception is being recognized from the Java service, since my branch steps in to...
Read more >Process | Node.js v20.5.1 Documentation
Exceptions thrown from within the event handler will not be caught. Instead the process will exit with a non-zero exit code and the...
Read more >Exit a Process with sys.exit() in Python
When the sys.exit() function is called, a SystemExit exception is raised in the main thread. The main thread terminates.
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

This fix seems to fix this issue, so we can close it.
@MartinRothschink Thanks for the link. I’ve found another workaround: implementing own
Mainmethod and callingEnvironment.Exitmanually after app finished working:It helps, because the exception/hang I’m experiencing happens after the return from
Main(call stack below). So, by callingExitI skip the post-Main phase.But I believe my approach has its drawbacks, since after
Environment.Exit(0)is called, the process immediately exists and so the normal runtime workflow (proper releasing of the runtime resources etc) doesn’t happen.