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
Just Close() all active/existing forms and the application should exit. ok.
The proper method would be 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.
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With