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

3
  • You're showing Form1 on last line, isn't it the reason?
    – CharlesB
    May 13, 2011 at 11:36
  • Why would you want to start an application without even run it really?
    – John xyz
    May 13, 2011 at 11:37
  • 1
    Hi all, this is only an example. I handle some functions before showing form. And in function, I call Application.Exit() but I wonder why application doesn't exit immediately.
    – Leo Vo
    May 13, 2011 at 15:25

7 Answers 7

53

Well logically it cannot work. The Application cannot exit if it is not running.

It would be better to use Environment.Exit here.

1
  • Precisely, Environment.Exit(int returnCode), where returnCode = 0 if all is good.
    – Fredy
    Sep 26, 2022 at 19:46
23

Application.Exit says:

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

The problem is the message pump for your application wont start until after you call Run and the Form is created.

The Exit method is typically called from within a message loop, and forces Run to return.

6
  • @SwDevMan81: After the call? When Run returns the message pump is already gone ;p
    – leppie
    May 13, 2011 at 11:37
  • You don't have to put Application.Exit(); in this main function. The application exits automatically when Form1 is closed.
    – John xyz
    May 13, 2011 at 11:38
  • My code is an example. I will handle some function before calling to show form. Example: I check to update for my program. I need a good solution to exit application immediately and avoid calling to next line. Thanks.
    – Leo Vo
    May 13, 2011 at 15:27
  • After calling Application.Exit(), I have any methods (or properties) to check Application.Exit() was called. EX: BackgroundWorker object have a method CancelAsync and I can check it's state using property CancellationPending = true.
    – Leo Vo
    May 13, 2011 at 16:02
  • See: stackoverflow.com/questions/2314072/…
    – SwDevMan81
    May 13, 2011 at 16:48
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.

1
  • 3
    This also works if your application has no windows.
    – timelmer
    Jun 19, 2016 at 2:33
8

The code below should help you.

        if (Application.MessageLoop)
            Application.Exit();
        else
            Environment.Exit(1);
6

Add a return; statement instead of Application.Exit();

1
  • By far the simplest solution, and it uses native c++ logic. Worked. Thanks!
    – Glitch
    Aug 10, 2015 at 11:19
2

The application is not even running at that point. Calling exit has no effect. In the main function you want to return. Like this:

    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        return;
        Application.Run(new Form1());
    }
1

According to the MSDN, Application.Exit()

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

Since there are no application windows yet, nothing happens (as I understand it). Then you start the application afterwards, because Application.Exit() does not as you think stop program execution.

I assume your problem is not actually as trivially simple as you described in the question - in any case simply return;ing without ever calling Application.Run() is probably the solution.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.