正确的方式关闭WPF GUI应用程序:GetCurrentProcess()。Kill(),Environment.Exit(0)或this.Shutdown() [英] Right way to close WPF GUI application: GetCurrentProcess().Kill(), Environment.Exit(0) or this.Shutdown()
问题描述
我的基于桌面的WPF 4.0(C#.Net 4.0)程序与SQL Server数据库一起使用。每次当我运行我的应用程序,它创建一个连接到SQL Server通过ADO.NET实体框架,如果SQL Server无法访问它抛出一个异常,并显示MessageBox通知。
My GUI desktop-based WPF 4.0 (C# .Net 4.0) program works with SQL Server database. Each time when I run my application it creates a connection to SQL Server via ADO.NET Entity Framework and if SQL Server is not reachable it throws an exception and shows MessageBox with notification.
现在我想让用户阅读这个消息后应用程序将关闭。我发现了三种方法:
Now I want that after user read this message application will shut down. I found three ways to do this:
Process.GetCurrentProcess().Kill();
或
this.Shutdown(); // Application.Current.Shutdown()
或
System.Environment.Exit(0);
所有这些都很好,并做我所需要的 - 关闭应用程序和杀死应用程序的过程在Windows任务管理器
All of them work fine and do what I need — close application and kill application's process in Windows Task Manager.
我想知道:
- 它们之间有什么区别?
- 哪种方式会更快地关闭我的申请?
- 我应该使用哪种方式关闭申请?
- 是
Application.Current.Shutdown()
和this.Shutdown()
同样关闭应用程序吗? >
- What is the difference between them?
- Which way will close my application faster?
- Which way to close application should I use?
- Is
Application.Current.Shutdown()
andthis.Shutdown()
the same way to close application?
或者,还有另一种更合适的方式来关闭WPF GUI应用程序?
Or maybe there is another, more suitable, way to close a WPF GUI application?
Application.Exit()
不适用于我,因为我得到的错误:
Application.Exit()
doesn't work for me as I get the error:
事件
System.Windows.Application.Exit
只能出现在+ =或 - =
The event '
System.Windows.Application.Exit
' can only appear on the left-hand side of += or -=
感谢。
推荐答案
.Current.Shutdown()
是关闭应用程序的正确方法。一般来说,因为触发您可以处理的退出事件更多
Application.Current.Shutdown()
is the proper way to shutdown an application. Generally because fire the exit events that you can handle more
Process.GetCurrentProcess()。当你想杀死应用程序时,应该使用Kill()
。 更多
Ad1。这些方法的性质是完全不同的。关闭进程可以暂停结束一些操作,杀死应用程序关闭。
Ad1. The nature of those methods are totally different. The shutdown process can be paused to end some operations, kill force the application to close.
Ad2。可能 Kill()
将是最快的方式,但这是类似内核恐慌的东西。
Ad2. Probably Kill()
will be the fastest way, but this is something like kernel panic.
Ad3。关闭,因为它触发关闭事件
Ad3. Shutdown because it fires the close event
Ad4。这取决于这
是什么。
Ad4. That depend what this
is.
这篇关于正确的方式关闭WPF GUI应用程序:GetCurrentProcess()。Kill(),Environment.Exit(0)或this.Shutdown()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!