You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Updating the Closing event handlers to FormClosing event handlers doesn't work with Application.Exit() sometimes #14924
Did it work in any of the earlier releases of .NET Core or .NET 5+?
Unknown
Issue description
When a Window Forms main form creates a second form, Application.Exit() doesn't work with the FormClosing event but does work with the Closing event when the event argument Cancel value is set to true by the second form.
Run Test 2 below to see the problem (the ReadMe.txt file gives complete instructions for the comparing the four different tests. Note that the ReadMe.txt file Test2 instructions, step 3 incorrectly says "Select Test4". It should say "Select Test2" like the instructions below.
Test 2 ([fails] uses Application.Exit with FormClosing [both main and secondary forms])
Run WinFormsExitTest
Click on the "Select Test" menu
Select "Test2 (FormClosing, FormClosing)"
Close the Form2 window which contains the text "This Is A Cached Window"
Click on the "Test" menu
Select "Exit"
The program will NOT exit
Click on the program's close button (the "X")
The application will exit
Thanks for the detailed repro. Application.Exit() raises the FormClosing event for all open forms. If any form sets e.Cancel = true, the application shutdown process is canceled and the application remains running.
In the repro, Form2.FormClosing contains logic similar to: this.Hide(); e.Cancel = true;
When Application.Exit() reaches Form2, the close operation is canceled by this handler, preventing the application from exiting.
A common pattern for cached or reusable windows is to cancel only user-initiated close operations while allowing application shutdown:
This preserves the cached-window behavior when the user clicks the window close button, while still allowing Application.Exit() to close the application successfully.
Based on the repro provided, the observed behavior appears to be the result of the FormClosing handler unconditionally canceling the close operation rather than an issue with Application.Exit() itself.
So this appears to be expected behavior rather than a WinForms runtime bug: the FormClosing handler cancels all close requests, including CloseReason.ApplicationExitCall.
@SimonZhao888, I have been looking into this together with Daniel in person, when we met at VSLive last month. We were thinking at the time of something completely different as the root cause, until recently. My first thoughts when I read this now were exactly yours. But then again, there might be something more subtle going on. So, just to be sure, let's take a closer look at that, and keep it open for now.
@DanLBarr, I was out sick the last week, so I have to do quite some catching up to do. So, probably mid next week, I hope I caught up enough to take a closer look at this?
@SimonZhao888, In one of our applications checking e.CloseReason wouldn't work. The application is a 2D graphics editor, and if a user has edited the model, hasn't saved the changes, and clicks the Windows close button ("X") on the main form, checking for unsaved changes in Form1.FormClosing won't work because Form2.FormClosing is called first. That means the Form2 handler doesn't have the information it needs to cancel the close.
The only simple ways I currently see to solve this problem are:
Use the old, obsolete, Closing events with Application.Exit()
Use Form.Close() instead of Application.Exit() when using Form.FormClosing event handlers
Change Application.Exit() and Form.FormClosing to work like Application.Exit() and Form.Closing for this case (may or may not be simple)
Document that Form.Close() should be used instead of Application.Exit() when using Form.FormClosing along with the reasons for that. This would need to be done in all the appropriate places.
I would guess that other customers will run (or maybe have run) into this problem. An internet search shows that Application.Exit() still seems to be used for closing WinForms applications, and if they have a case similar to ours and switch to the FormClosing event, they will have the same problem. We ran into it because I decided that, since Closing has been obsoleted for a while, it was time to update to FormClosing.
@KlausLoeffelmann, Thank you for looking into this! I'm glad I was able to talk to you about this issue in person. I enjoyed talking with you both this year's and last year's VSLive! I got sick the day I left for home after VSLive. Sorry you got sick!
Here is a little more detail about our actual editor application:
There is a main Form which contains two custom UserControl instances that display our graphics
one UserControl for editing the graphics statically (can include Windows Forms controls and/or WPF controls)
one UserControl for previewing the graphics dynamically driven by variables
There are multiple miscellaneous Forms
various "dialogs"
custom floating toolstrips (inherited from ToolStrip) each contained in a Form so they can float independent from the main Form. They can dock in the main Form or be dragged away to be completely free floating.
Activity
DanLBarr commentedon Aug 24, 2026
See the ReadMe.txt file for the steps to reproduce the issue.
Form1_cs.txt
Form1_Designer_cs.txt
Form1_resx.txt
Form2_cs.txt
Form2_Designer_cs.txt
Form2_resx.txt
Program_cs.txt
ReadMe.txt
WinFormsExitTest_csproj.txt
WinFormsExitTest_slnx.txt
WinFormsExitTest_csproj_user.txt
DanLBarr commentedon Aug 24, 2026
Test Procedure
Test 2 ([fails] uses Application.Exit with FormClosing [both main and secondary forms])
The program will NOT exit
The application will exit
SimonZhao888 commentedon Aug 24, 2026
Thanks for the detailed repro. Application.Exit() raises the FormClosing event for all open forms. If any form sets e.Cancel = true, the application shutdown process is canceled and the application remains running.
In the repro, Form2.FormClosing contains logic similar to:
this.Hide(); e.Cancel = true;When Application.Exit() reaches Form2, the close operation is canceled by this handler, preventing the application from exiting.
A common pattern for cached or reusable windows is to cancel only user-initiated close operations while allowing application shutdown:
This preserves the cached-window behavior when the user clicks the window close button, while still allowing Application.Exit() to close the application successfully.
Based on the repro provided, the observed behavior appears to be the result of the FormClosing handler unconditionally canceling the close operation rather than an issue with Application.Exit() itself.
So this appears to be expected behavior rather than a WinForms runtime bug: the FormClosing handler cancels all close requests, including CloseReason.ApplicationExitCall.
KlausLoeffelmann commentedon Aug 24, 2026
@SimonZhao888, I have been looking into this together with Daniel in person, when we met at VSLive last month. We were thinking at the time of something completely different as the root cause, until recently. My first thoughts when I read this now were exactly yours. But then again, there might be something more subtle going on. So, just to be sure, let's take a closer look at that, and keep it open for now.
@DanLBarr, I was out sick the last week, so I have to do quite some catching up to do. So, probably mid next week, I hope I caught up enough to take a closer look at this?
Take care - was good talking to you!
DanLBarr commentedon Aug 24, 2026
@SimonZhao888, In one of our applications checking e.CloseReason wouldn't work. The application is a 2D graphics editor, and if a user has edited the model, hasn't saved the changes, and clicks the Windows close button ("X") on the main form, checking for unsaved changes in Form1.FormClosing won't work because Form2.FormClosing is called first. That means the Form2 handler doesn't have the information it needs to cancel the close.
The only simple ways I currently see to solve this problem are:
I would guess that other customers will run (or maybe have run) into this problem. An internet search shows that Application.Exit() still seems to be used for closing WinForms applications, and if they have a case similar to ours and switch to the FormClosing event, they will have the same problem. We ran into it because I decided that, since Closing has been obsoleted for a while, it was time to update to FormClosing.
@KlausLoeffelmann, Thank you for looking into this! I'm glad I was able to talk to you about this issue in person. I enjoyed talking with you both this year's and last year's VSLive! I got sick the day I left for home after VSLive. Sorry you got sick!
DanLBarr commentedon Aug 25, 2026
Here is a little more detail about our actual editor application:
There is a main Form which contains two custom UserControl instances that display our graphics
There are multiple miscellaneous Forms
Here is a screen shot:
