I assumed that Application.Exit
causes app to exit imemdiately but I can see that according to example below, it will exit after the for cycle ends. Also when this command will force the app to exit?
for (int I = 0; I < 1000; I++)
{
if (I == 1)
Application.Exit();
}
As you can see here, this method "Informs all message pumps that they must terminate" and "This method does not force the application to exit. "
Application.Exit() will cause the application to exit once it returns to the underlying message pump. If you're running code in the UI thread, this won't be until you return from whatever UI method you're in (such as a button click handler.)
Is the question "how can I exit right now?" If so, go with Environment.FailFast -- it is the fastest way out, and as an extra bonus, you can leave an entry in the event log. As it says on MSDN,
This method terminates a process without running any active try/finally blocks or finalizers.
The FailFast method writes the message string to the Windows Application event log, creates a dump of your application, and then terminates the current process. The message string is also included in error reporting to Microsoft.
Use the FailFast method instead of the Exit method to terminate your application if the state of your application is damaged beyond repair, and executing your application's try/finally blocks and finalizers will corrupt program resources.
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