Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Application.Exit() not working

Tags:

c#

.net

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

like image 508
Leo Vo Avatar asked May 13 '11 11:05

Leo Vo


People also ask

How do I exit an application in VB net?

Just Close() all active/existing forms and the application should exit. ok.

How do I exit Winforms application?

The proper method would be Application. Exit() .

What is the purpose of using below command application exit ();?

Application. Exit( )". This method internally informs all message loops in application that you must terminate. Then this method wait to close all application windows till the message loops have been processed.

What is the meaning of application exit in Windows?

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.

How to exit the application if it is not running?

The Application cannot exit if it is not running. It would be better to use Environment.Exit here. 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.

Does this method force the application to exit from the thread?

This method does not necessarily force the application to exit. The Exit method is typically called from within a message loop, and forces Run to return. To exit a message loop for the current thread only, call ExitThread.

What is the use of exit method in Java?

The Exit method stops all running message loops on all threads and closes all windows of the application. This method does not necessarily force the application to exit. The Exit method is typically called from within a message loop, and forces Run to return.


2 Answers

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

It would be better to use Environment.Exit here.

like image 183
leppie Avatar answered Sep 28 '22 01:09

leppie


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.

like image 45
SwDevMan81 Avatar answered Sep 28 '22 00:09

SwDevMan81