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

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

问题描述

我的GUI桌面,基于WPF 4.0(C#.NET 4.0)程序使用SQL Server数据库。每次当我运行我的应用程序时,它会创建通过ADO.NET实体框架的SQL Server的连接,如果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.

我想知道的:


  1. 它们之间有什么区别?

  2. 哪种方式将关闭我的应用程序更快吗?

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

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

  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?

或者,也许还有另一个更合适,这样收一个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()。杀()当你想杀死应用程序应该使用。

Process.GetCurrentProcess().Kill() should be used when you want to kill the application. more

广告1。这些方法的性质是完全不同的。关闭过程可暂停结束某些操作,杀强制关闭应用程序。

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的。也许杀()将是最快的方式,但是这有点像内核崩溃。

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()杀死(),Environment.Exit(0)或this.Shutdown()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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