开发者

App.Exit does not exit immediately?

开发者 https://www.devze.com 2023-03-09 23:09 出处:网络
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 comma开发者_开发百科nd will force th

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 comma开发者_开发百科nd 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.


From MSDN:

Informs all message pumps that they must terminate, and then closes all application windows after the messages have been processed.

-

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.


Check MSDN

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. To exit a message loop for the current thread only, call ExitThread.

0

精彩评论

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

关注公众号