Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8247395
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T22:50:10+00:00 2026-06-07T22:50:10+00:00

When I read the docs on MSDN for Application.Exit(), it says: Informs all message

  • 0

When I read the docs on MSDN for Application.Exit(), it says:

Informs all message pumps that they must terminate, and then closes all application windows after the messages have been processed.

In my understanding, to inform all message pump to terminate, this method would finally post a WM_QUIT message to the application message queue. And after posted the message, the method then would close each window(by MSDN). The problem is arising here, when this method try to close each window, the WM_QUIT message should have not been processed, but the MSDN said “it closes all windows after the messages have been processed”.

It seems the documentation is contradictory to my inference. What is the problem here, any help is greatly appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T22:50:12+00:00Added an answer on June 7, 2026 at 10:50 pm

    Interesting question; using ILSpy, let’s have a look at what Application.Exit() does:

    We see that the critical method is ExitInternal

    private static bool ExitInternal()
    {
        bool flag = false;
        lock (Application.internalSyncObject)
        {
            if (Application.exiting)
            {
                return false;
            }
            Application.exiting = true;
            try
            {
                if (Application.forms != null)
                {
                    foreach (Form form in Application.OpenFormsInternal)
                    {
                        if (form.RaiseFormClosingOnAppExit())
                        {
                            flag = true;
                            break;
                        }
                    }
                }
                if (!flag)
                {
                    if (Application.forms != null)
                    {
                        while (Application.OpenFormsInternal.Count > 0)
                        {
                            Application.OpenFormsInternal[0].RaiseFormClosedOnAppExit();
                        }
                    }
                    Application.ThreadContext.ExitApplication();
                }
            }
            finally
            {
                Application.exiting = false;
            }
        }
        return flag;
    }
    

    If everything goes well, the application will first close all forms, then close any forms it missed, and then, finally, it calls Application.ThreadContext.ExitApplication();

    As part of ExitApplication, we see the cleanup:

    private static void ExitCommon(bool disposing)
    {
        lock (Application.ThreadContext.tcInternalSyncObject)
        {
            if (Application.ThreadContext.contextHash != null)
            {
                Application.ThreadContext[] array = new Application.ThreadContext[Application.ThreadContext.contextHash.Values.Count];
                Application.ThreadContext.contextHash.Values.CopyTo(array, 0);
                for (int i = 0; i < array.Length; i++)
                {
                    if (array[i].ApplicationContext != null)
                    {
                        array[i].ApplicationContext.ExitThread();
                    }
                    else
                    {
                        array[i].Dispose(disposing);
                    }
                }
            }
        }
    }
    
    // System.Windows.Forms.ApplicationContext
    /// <summary>Terminates the message loop of the thread.</summary>
    /// <filterpriority>1</filterpriority>
    public void ExitThread()
    {
        this.ExitThreadCore();
    }
    

    What does ExitThreadCore do?

    Well, it doesn’t directly kill the thread, but it does start the process:

    ExitThread and ExitThreadCore do not actually cause the thread to
    terminate. These methods raise the ThreadExit event to which the
    Application object listens. The Application object then terminates the
    thread.

    However, the really interesting bit seems to happen in array[i].Dispose(disposing)

    As part of this method, we see:

    if (this.messageLoopCount > 0 && postQuit)
    {
        this.PostQuit();
    }
    

    PostQuit() is what sends the WM_QUIT message. So we should also consider when Application.ThreadContext.Dispose is called, and it is always seems to be after the forms have closed, although I’m happy to be corrected there.

    So the order seems to be close all forms, then send the WM_QUIT message. I think you are right, the documentation may actually have the events in the wrong order…

    It also confirms another side effect we often see; when an application is closed but there is still a thread running in the background, the exe will still be in the list of running applications. The forms have been closed, but there is still that rogue thread, humming along preventing the Exit() from completing.

    As Tergiver mentions:

    A thread is either a background thread or a foreground thread.
    Background threads are identical to foreground threads, except that
    background threads do not prevent a process from terminating. Once all
    foreground threads belonging to a process have terminated, the common
    language runtime ends the process. Any remaining background threads
    are stopped and do not complete.

    (from Thread.IsBackgroundThread)

    I also wondered what Environment.Exit does:

    [SecurityCritical, SuppressUnmanagedCodeSecurity]
    [DllImport("QCall", CharSet = CharSet.Unicode)]
    internal static extern void _Exit(int exitCode);
    

    It effectively calls out to the OS to kill the process; this will terminate all windows with little grace; the OnFormClosing will probably never get to fire for example. As part of this wholesale termination, it will also [I hesitate to use attempt as I’ve never seen it fail] kill any threads including the ‘main’ thread the message loop is running on.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have read all the docs I can get my hands on and google'd
I read somewhere in the docs that WebDriver API's are non blocking (except for
I am writing a simple client-server application, and looking the MSDN docs, I came
I read some docs about md5, it said that its 128 bits, but why
The MSDN docs for volatile in Visual C++ indicate that writes have release semantics
I read that docs.jquery.com uses wiki to do the API documentation. I have been
I've read many docs about thread states, some of them tells that there is
I've searched Stack for ages, read the MSDN docs and used Bing but cannot
I have read the docs and everything but I'm very confused. I never needed
I've read some docs about the .NET Garbage Collector but i still have some

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.