Search code examples
c#wpfdialogmonitorshutdown

how to know the application is shutting down


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


Solution

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