Skip to content

Updating the Closing event handlers to FormClosing event handlers doesn't work with Application.Exit() sometimes #14924

Open

Description

@DanLBarr

.NET version

.NET 10.0

Did it work in .NET Framework?

No

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.

Steps to reproduce

Activity

  1. added
    untriagedThe team needs to look at this issue in the next triage
    on Aug 24, 2026
  2. DanLBarr commented on Aug 24, 2026

    @DanLBarr
    Author

    Test Procedure

    • Build WinFormsExitTest with Visual Studio 2026
    • 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])

    1. Run WinFormsExitTest
    2. Click on the "Select Test" menu
    3. Select "Test2 (FormClosing, FormClosing)"
    4. Close the Form2 window which contains the text "This Is A Cached Window"
    5. Click on the "Test" menu
    6. Select "Exit"
      The program will NOT exit
    7. Click on the program's close button (the "X")
      The application will exit
  3. SimonZhao888 commented on Aug 24, 2026

    @SimonZhao888
    Member

    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:

    private void Form2_FormClosing(object sender, FormClosingEventArgs e)
    {
      if (e.CloseReason == CloseReason.UserClosing)
      {
        Hide();
        e.Cancel = true;
      }
    }
    

    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.

  4. KlausLoeffelmann commented on Aug 24, 2026

    @KlausLoeffelmann
    Member

    @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!

  5. added this to the 11.0-GA milestone on Aug 24, 2026
  6. DanLBarr commented on Aug 24, 2026

    @DanLBarr
    Author

    @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:

    1. Use the old, obsolete, Closing events with Application.Exit()
    2. Use Form.Close() instead of Application.Exit() when using Form.FormClosing event handlers
    3. Change Application.Exit() and Form.FormClosing to work like Application.Exit() and Form.Closing for this case (may or may not be simple)
    4. 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!

  7. DanLBarr commented on Aug 25, 2026

    @DanLBarr
    Author

    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.

    Here is a screen shot:
    Image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

untriagedThe team needs to look at this issue in the next triage

Type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions