Alex Rivera | Logout

Application.Exit() not working

Asked 2011-05-13T11:33:53.670
35
static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Exit();
            Application.Run(new Form1());
        }

Why after calling Application.Exit(), application doesn't exit immediately? After this line, Form1 still shows. How to exit application immediately. Thanks.

*Notes:*this is only an example. I handle some functions before showing form. And in functions, I have a command code to call Application.Exit() but I wonder why application doesn't exit immediately.

I'm using .NET Framework 4.0

Edit
Report

1 Answer

17

Application.Exit is the "nice way" of shutting down a program. Outstanding windows messages are processed and only then are all windows closed. This also means, that Application.Exit only has meaning when the Windows message loop is running, that is, when the program is inside Application.Run. You call Application.Exit before Application.Run, so there is no message loop to exit.

From the documentation:

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

What you are looking for is probably System.Environment.Exit, which more or less kills the process. Environment.Exit still does some clean-up. If you need to terminate the process immediately, you can use System.Environment.FailFast.

answered 2011-05-13T11:40:48.117

Your Answer