Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

App.Exit does not exit immediately?

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();
}
like image 637
Mirial Avatar asked Jun 01 '11 13:06

Mirial


3 Answers

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. "

like image 93
Otávio Décio Avatar answered Nov 15 '22 00:11

Otávio Décio


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.)

like image 44
Dan Bryant Avatar answered Nov 15 '22 00:11

Dan Bryant


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.

like image 3
Kate Gregory Avatar answered Nov 14 '22 22:11

Kate Gregory