开发者

Closing Applications

开发者 https://www.devze.com 2023-03-29 14:55 出处:网络
What is best practice when closing a C# application?开发者_如何学C I have read that you can use:

What is best practice when closing a C# application?开发者_如何学C

I have read that you can use:

Environment.Exit(0); or Application.Exit();

But what is the difference?

Furthermore, with regards to Environment.Exit(0), I have used exit codes before when working with Java but have never fully understood their purpose. What role do they play when exiting an application in C#?


System.Windows.Forms.Application.Exit() - Informs all message pumps that they must terminate, and then closes all application windows after the messages have been processed. This method stops all running message loops on all threads and closes all windows of the application. This method does not 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(). This is the call to use if you are running a Windows Forms application. As a general guideline, use this call if you have called System.Windows.Forms.Application.Run().

System.Environment.Exit(exitCode) - Terminates this process and gives the underlying operating system the specified exit code. This call requires that you have SecurityPermissionFlag.UnmanagedCode permissions. If you do not, a SecurityException error occurs. This is the call to use if you are running a console application.

I hope it is best to use Application.Exit

See also these links:

  • Application.Exit() vs Application.ExitThread() vs Environment.Exit()
  • http://geekswithblogs.net/mtreadwell/archive/2004/06/06/6123.aspx


Application.Exit is for Windows Forms applications - it informs all message pumps that they should terminate, waits for them to finish processing events and then terminates the application. Note that it doesn't necessarily force the application to exit.

Environment.Exit is applicable for all Windows applications, however it is mainly intended for use in console applications. It immediately terminates the process with the given exit code.

In general you should use Application.Exit in Windows Forms applications and Environment.Exit in console applications, (although I prefer to let the Main method / entry point run to completion rather than call Environment.Exit in console applications).

For more detail see the MSDN documentation.


What role do they play when exiting an application in C#?

The same as every other application. Basically they get returned to the caller. Irrelvant if ythe start was an iicon double click. Relevant is the call is a batch file that decides whether the app worked on the return code. SO, unless you write a program that needs this, the return dcode IS irrelevant.

But what is the difference?

One comes from environment one from the System.Windows.Forms?.Application. Functionall there should not bbe a lot of difference.


for me best solotion this is

 Thread.CurrentThread.Abort();

and force close app.


Try this

System.Windows.Application.Current.Shutdown();

It is work also with NotifyIcon. Place this code in App.xaml.cs '

protected override void OnExit(ExitEventArgs e)
    {
        base.OnExit(e);
        App.nIcon.Visible = false;
    } 
0

精彩评论

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