关闭WPF GUI应用程序的正确方法:GetCurrentProcess()。Kill(),Environment.Exit(0)或this.Shutdown() [英] Right way to close WPF GUI application: GetCurrentProcess().Kill(), Environment.Exit(0) or this.Shutdown()
问题描述
现在我想要在用户读取此消息后,应用程序将关闭。我找到了三种方法:
Process.GetCurrentProcess()。Kill();
或
this.Shutdown(); // Application.Current.Shutdown()
或
System.Environment.Exit(0);
所有这些都可以正常工作,做我需要的 - 关闭应用程序并在Windows任务管理器中杀死应用程序的进程
我想知道:
- 他们有什么区别?
- 哪种方式能够更快地关闭我的应用程序?
- 应该使用哪种方式来关闭应用程序?
- 是
Application.Current.Shutdown()
和this.Shutdown()
以相同的方式关闭应用程序? >
或者还有另一种更合适的方法来关闭WPF GUI应用程序?
Application.Exit()
对我而言不起作用,因为我收到错误:
事件'
$ b $的左侧bSystem.Windows.Application.Exit
'只能出现在+ =或 - =
谢谢。
.Current.Shutdown()
是pr操作方式来关闭应用程序。通常是因为触发可以处理的退出事件更多
Process.GetCurrentProcess()。当您要杀死应用程序时,应使用Kill()
。 更多
Ad1。这些方法的本质是完全不一样的。关机过程可以暂停结束一些操作,杀死强制应用程序关闭。
Ad2。可能 Kill()
将是最快的方式,但这是内核恐慌。
Ad3。关闭因为它触发关闭事件
Ad4。这取决于这个
是什么。
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();
or
this.Shutdown(); // Application.Current.Shutdown()
or
System.Environment.Exit(0);
All of them work fine and do what I need — close application and kill application's process in Windows Task Manager.
I want to know:
- 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?
Or maybe there is another, more suitable, way to close a WPF GUI application?
Application.Exit()
doesn't work for me as I get the error:
The event '
System.Windows.Application.Exit
' can only appear on the left-hand side of += or -=
Thanks.
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()
should be used when you want to kill the application. more
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. Probably Kill()
will be the fastest way, but this is something like kernel panic.
Ad3. Shutdown because it fires the close event
Ad4. That depend what this
is.
这篇关于关闭WPF GUI应用程序的正确方法:GetCurrentProcess()。Kill(),Environment.Exit(0)或this.Shutdown()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!