how to know the application is shutting down

家住魔仙堡 提交于 2019-12-23 17:41:18

问题


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

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?

Solution:

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.

Problem solved


回答1:


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.

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.




回答2:


have you checked the FormClosing event?




回答3:


I still have not got the knowledge of WPF but I do know from winforms, there is an event Closing from the Form class, which you can trap and present the dialog 'This application is exiting, do you wish to continue etc', and set the Cancel flag to true to abort closing the form, also there is ApplicationExit event from the Application class, you can trap that if you want to handle the shutting down of the application.

Hope this helps, Best regards, Tom.



来源:https://stackoverflow.com/questions/2113606/how-to-know-the-application-is-shutting-down

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!