简体   繁体   中英

Application.Exit(): Does it start a new thread (my program continues execution in parallel)?

I have a multi-threaded application, and both in the main UI thread and in other background threads, I have stop-methods that call Application.Exit().

The weird thing is that after the call, the program continues on to the next line (and then eventually exits). The obvious problem with this can be explained with the simple code below:

if (XYZ) Application.Exit();
Globals.Instance.LoggerDictionary["ApplicationLog"].Log("Bla bla...");

And this:

    private void Form1_FormClosing(object sender, FormClosingEventArgs e)
    {
        var button = sender as Button;
        if (button != null && string.Equals(button.Name, @"CloseButton"))
        {
            //FormClosing event raised by a user created button action
        }
        else
        {
            //FormClosing event raised by program or the X in top right corner
            Globals.Instance.LoggerDictionary["ApplicationLog"].Dispose();
            Globals.Instance.LoggerDictionary["OtherLog"].Dispose();
            MemoryHandler.Instance.Dispose();
        }
    }

As seen the FormClosing method ensures that cleanup of unmanaged resources is done. So, in many cases the application moves on to the line below Application.Exit(), and, in the example above, try to write something to the Log, which has already been disposed by the FormClosing() method.

My question is therefore: Does Application.Exit() spawn a new thread where FormClosing() is run from? If not, then why does it continue on?

http://msdn.microsoft.com/en-us/library/system.windows.forms.application.exit(v=vs.110).aspx

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

This method does not terminate the process. Instant termination is rarely a useful shutdown model because it is unclear what useful shutdown actions would be skipped.

If this method does not terminate the process then clearly your code must continue to run.

This is normal and the right way to shut down the app. If you want to skip certain logic, set a flag bool isShuttingDown and react to it.

No, it does not start a new thread. It also doesn't immediately kill the UI thread. The whole point of Application.Exit is to gracefully exit the application. All forms will be asked to be closed, resources will be cleaned up, all pumped through the main application loop. When the application loop next becomes idle, rather than waiting for more messages, it will instead stop pumping messages and the application will continue running after the call to Appliaction.Run that created the message loop in the first place. That thread can then go on to do whatever (usually end; unless you've added more code).

No it does not spawn a extra thread. Looking at the reference source inside the function it will call each form's FormClosing event, it then returns control to the caller. Once the caller completes and control returns to the message loop that is when the program will actually shut down.

All of this happens on the single UI thread.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM