question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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.

image

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:closed
  • Created a year ago
  • Reactions:1
  • Comments:17 (9 by maintainers)

github_iconTop GitHub Comments

1reaction
Daniellledcommented, Oct 26, 2022

Interestingly enough if I modify the App.xaml code to not call SessionEnding=“Application_SessionEnding” zero files are created.

<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"
             
             ShutdownMode="OnMainWindowClose">
    <Application.Resources>
         
    </Application.Resources>
</Application>

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.

1reaction
Daniellledcommented, Oct 20, 2022

If you have 64-bit preferred then yes. But I can reproduce it under debugger only anyway, and I suspect the debugger process is just shutting it down in panic. Sounds like I don’t really have repro then. Just to rule out the obvious - it’s a normal desktop app, correct? You don’t start as a console or service…

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found