关闭WPF GUI应用程序的正确方法:GetCurrentProcess()。Kill(),Environment.Exit(0)或this.Shutdown() [英] Right way to close WPF GUI application: GetCurrentProcess().Kill(), Environment.Exit(0) or this.Shutdown()

查看:315
本文介绍了关闭WPF GUI应用程序的正确方法:GetCurrentProcess()。Kill(),Environment.Exit(0)或this.Shutdown()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的基于GUI桌面的WPF 4.0(C#.Net 4.0)程序与SQL Server数据库配合使用。每次运行我的应用程序时,都会通过ADO.NET Entity Framework创建与SQL Server的连接,如果SQL Server不可访问,则会抛出异常并显示带有通知的MessageBox。



现在我想要在用户读取此消息后,应用程序将关闭。我找到了三种方法:

  Process.GetCurrentProcess()。Kill(); 

  this.Shutdown(); // Application.Current.Shutdown()

  System.Environment.Exit(0); 

所有这些都可以正常工作,做我需要的 - 关闭应用程序并在Windows任务管理器中杀死应用程序的进程



我想知道:


  1. 他们有什么区别?

  2. 哪种方式能够更快地关闭我的应用程序?

  3. 应该使用哪种方式来关闭应用程序?

  4. Application.Current.Shutdown() this.Shutdown()以相同的方式关闭应用程序? >

或者还有另一种更合适的方法来关闭WPF GUI应用程序?



Application.Exit()对我而言不起作用,因为我收到错误:


事件' System.Windows.Application.Exit '只能出现在+ =或 - =

$ b $的左侧b

谢谢。

解决方案

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

  1. What is the difference between them?
  2. Which way will close my application faster?
  3. Which way to close application should I use?
  4. Is Application.Current.Shutdown() and this.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屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆