wpf: Application Exit not called on windows restart
- .NET Core Version: 6.0
- Windows version: 19044.2130
- Does the bug reproduce also in WPF for .NET Framework 4.8?: Yes
- Is this bug related specifically to tooling in Visual Studio (e.g. XAML Designer, Code editing, etc…)? No
Problem description: When a WPF application is running and the I restart Windows. The Application Exit method defined in App.xaml is not called. Additionally when I override the OnExit method it is also not called.
I have (I think) turned off Fast Startup and the issue still occurs.
Actual behavior: OnExit override and Application Exit method are not called when restarting Windows with application running and visible.
Expected behavior: OnExit override and Application Exit methods are invoked when Windows is restarted.
Minimal repro:
App.xaml
<Application x:Class="ShutdownTest.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:ShutdownTest"
StartupUri="MainWindow.xaml"
Exit="Application_Exit"
SessionEnding="Application_SessionEnding"
ShutdownMode="OnMainWindowClose">
<Application.Resources>
</Application.Resources>
</Application>
App.xaml.cs
public partial class App : Application
{
protected override void OnExit(ExitEventArgs e)
{
using(var sw = new StreamWriter(@"c:\logs\test.txt"))
{
sw.WriteLine($"{nameof(OnExit)} - called");
}
base.OnExit(e);
}
private void Application_Exit(object sender, ExitEventArgs e)
{
using (var sw = new StreamWriter(@"c:\logs\test2.txt"))
{
sw.WriteLine($"{nameof(Application_Exit)} - called");
}
}
private void Application_SessionEnding(object sender, SessionEndingCancelEventArgs e)
{
using (var sw = new StreamWriter(@"c:\logs\test3.txt"))
{
sw.WriteLine($"{nameof(Application_SessionEnding)} - called");
}
}
}
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Reactions: 1
- Comments: 17 (9 by maintainers)
Interestingly enough if I modify the App.xaml code to not call SessionEnding=“Application_SessionEnding” zero files are created.
It is almost like the behavior is as follows.
If a SessionEnding method is defined the SessionEnding method is fully invoked. And the OnExit override is partially invoked but cut short by the OS. While the Exit=“Application_Exit” method defined in App.xaml is never invoked.
If a SessionEnding method is not defined. The OnExit override method is not called as well as the Exit=“Application_Exit” method defined in App.xaml.
I just went into Microsoft Visual Studio Enterprise 2022 (64-bit) Version 17.0.1 and created a new WPF project and added the modifications posted in the issue.
So I would say it is a normal desktop app.
Also can you clarify whether the behavior you are seeing is that the system actually restarts and you don’t get called and there is no exceptions in the Event Viewer? And you check that by not seeing the files after restart, correct?
https://stackoverflow.com/questions/47141816/application-exit-event-not-called-on-logoff-or-shutdown