关闭 WPF GUI 应用程序的正确方法:GetCurrentProcess().Kill()、Environment.Exit(0) 或 this.Shutdown()

发布于 2024-09-26 22:58:50 字数 903 浏览 6 评论 0原文

我的基于 GUI 桌面的 WPF 4.0 (C# .Net 4.0) 程序与 SQL Server 数据库配合使用。每次运行应用程序时,它都会通过 ADO.NET 实体框架创建与 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”只能出现在 += 或 -= 的左侧

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.

My questions:

  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 -=

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

淡淡離愁欲言轉身 2024-10-03 22:58:50

Application.Current.Shutdown() 是关闭应用程序的正确方法。一般来说,因为它会触发退出事件,所以您可以处理

当您想尽快终止应用程序时,应使用 Process.GetCurrentProcess().Kill() ,进一步信息

这些方法的性质完全不同:

  • Shutdown() 可以暂停以结束某些操作
  • Kill() 强制应用程序立即关闭,尽快 可能

,< code>Kill() 将是最快的方法,但这有点像内核恐慌。

Application.Current.Shutdown() is the proper way to shutdown an application. Generally, because it fires the exit events, that you can handle, further info.

Process.GetCurrentProcess().Kill() should be used when you want to kill the application as soon as possible, further info.

The nature of those methods is totally different:

  • Shutdown() can be paused to end some operations
  • Kill() forces the application to close immediately, as soon as possible

Probably, Kill() will be the fastest way, but this is something like kernel panic.

蘑菇王子 2024-10-03 22:58:50

使用Application.Current.Shutdown();

App.xaml中添加ShutdownMode="OnMainWindowClose"

Use Application.Current.Shutdown();

Add ShutdownMode="OnMainWindowClose" in App.xaml

篱下浅笙歌 2024-10-03 22:58:50
private void ExitMenu_Click(object sender, RoutedEventArgs e)
    {
        Application.Current.Shutdown();
    }
private void ExitMenu_Click(object sender, RoutedEventArgs e)
    {
        Application.Current.Shutdown();
    }
各空 2024-10-03 22:58:50

@Damian Leszczyński - Vash 的回答几乎涵盖了您提出的 4 个具体问题。对于您关于 Application.Exit() 的最后一个问题,这是您可以订阅的事件,而不是您可以调用的方法。它应该像这样使用:

Application.Current.Exit += CurrentOnExit; 
//this.Exit += CurrentOnExit; would also work if you're in your main application class

...

private void CurrentOnExit(object sender, ExitEventArgs exitEventArgs)
{
    //do some final stuff here before the app shuts down
}

@Damian Leszczyński - Vash's answer pretty much covers the 4 specific questions you asked. For your final question on Application.Exit(), that's an event you can subscribe to, not a method that you can call. It should be used like this:

Application.Current.Exit += CurrentOnExit; 
//this.Exit += CurrentOnExit; would also work if you're in your main application class

...

private void CurrentOnExit(object sender, ExitEventArgs exitEventArgs)
{
    //do some final stuff here before the app shuts down
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文