Application Exit not called on windows restart
See original GitHub issue- .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");
}
}
}
Issue Analytics
- State:
- Created a year ago
- Reactions:1
- Comments:17 (9 by maintainers)
Top Results From Across the Web
c# - Application.Exit() not working
Exit only has meaning when the Windows message loop is running, that is, when the program is inside Application.Run . You call Application....
Read more >Application.Exit Event (System.Windows)
The Shutdown method of the Application object is called, ... Exit is not raised and the application continues running in accordance with the...
Read more >Application.Exit Method (System.Windows.Forms)
The Exit method stops all running message loops on all threads and closes all windows of the application. This method does not necessarily...
Read more >Why doesn't application.current.shutdown() work any longer?
The solution turns out to be embarrassingly simple. There was no return statement after the call to Application.Current.Shutdown() so the ...
Read more >Application.ApplicationExit Event (System.Windows.Forms)
This example demonstrates using the ApplicationExit event to know when the form positions should be persisted to the file, and when the FileStream...
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 FreeTop 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
Top GitHub Comments
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.