开发者

Application.Exit() not working

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 applic开发者_运维知识库ation doesn't exit immediately.

I'm using .NET Framework 4.0


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

It would be better to use Environment.Exit here.


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.


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.


The code below should help you.

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


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


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());
    }


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜