Skip to content

WPF 警惕使用 Dispatcher.InvokeShutdown 方法退出应用 将不触发 Application.Exit 事件

Updated: at 08:22,Created: at 07:34

这是一个比较让人困惑的一个点,我一直都以为 Dispatcher.InvokeShutdown 和 Application.Current.Shutdown 是完全等价的。但是后面发现了其实这两者还是有些不同的,感觉上是 Dispatcher.InvokeShutdown 系列方法有点点设计的问题,太过于为了让框架内的代码解耦导致了让上层开发者困惑。 推荐在退出应用时,尽量调用的是 Application.Current.Shutdown 方法,而不是 Dispatcher.InvokeShutdown 系列方法

先来说说 Dispatcher.InvokeShutdown 和 Application.Current.Shutdown 的区别。在调用 Application.Current.Shutdown 方法时,将会触发 Application.Current.Exit 事件,以及在框架内调用 Dispatcher.InvokeShutdown 方法,且触发 Dispatcher.ShutdownFinished 事件

在调用 Dispatcher.InvokeShutdown 系列方法,包括 Dispatcher.BeginInvokeShutdown 等时,将不触发 Application.Exit 事件,只触发 Dispatcher.ShutdownFinished 事件

如此可以看到,调用 Application.Current.Shutdown 方法是包含了 Dispatcher.InvokeShutdown 方法的,能比较符合开发者预期的触发各个退出事件

我写了一个 Demo 用来方便让大家了解这两个方法触发的差别,代码放在github 和 gitee 欢迎访问

可以通过如下方式获取本文的源代码,先创建一个空文件夹,接着使用命令行 cd 命令进入此空文件夹,在命令行里面输入以下代码,即可获取到本文的代码

git init
git remote add origin https://gitee.com/lindexi/lindexi_gd.git
git pull origin fe31b73707f46f753976c276bc3aea74e85f9127

以上使用的是 gitee 的源,如果 gitee 不能访问,请替换为 github 的源。请在命令行继续输入以下代码

git remote remove origin
git remote add origin https://github.com/lindexi/lindexi_gd.git
git pull origin fe31b73707f46f753976c276bc3aea74e85f9127

获取代码之后,进入 HoyebenawlerWegemnardicheba 文件夹


知识共享许可协议

原文链接: http://blog.lindexi.com/post/WPF-%E8%AD%A6%E6%83%95%E4%BD%BF%E7%94%A8-Dispatcher.InvokeShutdown-%E6%96%B9%E6%B3%95%E9%80%80%E5%87%BA%E5%BA%94%E7%94%A8-%E5%B0%86%E4%B8%8D%E8%A7%A6%E5%8F%91-Application.Exit-%E4%BA%8B%E4%BB%B6

本作品采用 知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议 进行许可。 欢迎转载、使用、重新发布,但务必保留文章署名 林德熙 (包含链接: https://blog.lindexi.com ),不得用于商业目的,基于本文修改后的作品务必以相同的许可发布。如有任何疑问,请与我 联系。