如何知道应用程序正在关闭 [英] how to know the application is shutting down

查看:197
本文介绍了如何知道应用程序正在关闭的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一个WPF应用程序。

There is a WPF application.

我要登录当应用程序被关闭。

i want to log when the application is closed.

但我不能修改应用程序(一些限制,只是因为业务)。

but i cannot modify the application (some restriction, just because business).

所以我创建一个无形的表单组件住现有的应用程序中,添加为一个dll库,所以现有的应用程序并不需要进行修改。

So i create an invisible form component live inside the existing application, add as a dll library, so the existing application do not need to be modified.

但问题是,如何我不可见组件知道应用程序正在关闭?

but the issue is, how can my invisible component know the application is shutting down?

有一些功能或事件处理程序,我可以使用

is there some function or event handler i can use?

解决方法:

有是一些事件可以做到这一点,

there are some event can do that,

卸载
闭幕
关闭

UnLoaded Closing Closed

所有这些三个事件将在主窗口将被关闭被解雇。

all these three events will be fired when the main windows is going to be closed.

问题解决了

推荐答案

System.Windows.Window 类有一个虚拟的 OnClosing(CancelEventArgs)方法,可以覆盖这一点,和结束可处理的事件。

The System.Windows.Window class has a virtual OnClosing(CancelEventArgs) method that can be overridden for this, and a Closing event that can be handled.

我通常捕获这些事件并传递到类,其中,可悲的是,不包含这样的方法或事件的信息。

I typically capture these events and pass the information on to the Page classes, which, sadly, contain no such methods or events.

每个窗口会火这些方法,当它被关闭,因此没有必要线向上父和子窗口之间的任何消息。而且,我相信,任务管理器将首先尝试做应用,这将导致关闭事件火的正常关机,除非别的东西,像一个对话框,阻止它。正在尝试后,才将TM询问是否异常结束是应用程序,这将很明显,规避交割事件来进行。

Each window will fire these methods when it is closed, so it is not necessary to wire-up any messages between parent and child windows. And, I believe, the Task Manager will first attempt to do a normal shut-down of the application, which will cause the close events to fire, unless something else, like a dialog box, prevents it. Only after that is attempted will TM ask if the an abnormal end is to be performed for the application, which will, obviously, circumvent the Closing event.

这篇关于如何知道应用程序正在关闭的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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