2

I have app with lot of windows and Im just curious is there (in WPF) something like global.asax which was in ASP.NET? I just want to clean some data after somebody shutdown application with alt-f4 for example.

Thanks for answers!

user13657
  • 745
  • 3
  • 17
  • 36
  • possible duplicate of [Catch windows shutdown event in a wpf application](http://stackoverflow.com/questions/7136573/catch-windows-shutdown-event-in-a-wpf-application) – Simon Oct 02 '12 at 12:15

1 Answers1

4

The rough equivalent of Global.asax in WPF is app.xaml/app.xaml.cs. You can override methods there to handle startup and shutdown.

For example, in your app.xaml.cs, you can add this:

    protected override void OnExit(ExitEventArgs e)
    {
        base.OnExit(e);

        // custom exit code here
    }
Dan Puzey
  • 33,626
  • 4
  • 73
  • 96
  • Thanks for answer. So when i have 30 windows, i need to put in each of them event 'application_shutdown' and redirect it to class for example? – user13657 Oct 02 '12 at 12:23
  • No, the class will call your code automatically on exit. See my edit for a sample. – Dan Puzey Oct 02 '12 at 12:31
  • One last question - how can i handle alt-f4 button pressed in app.xaml.cs? – user13657 Oct 02 '12 at 13:18
  • What do you mean by "handle?" – Dan Puzey Oct 02 '12 at 13:21
  • I mean i want to know when user press alt + f4 (doesnt matter in which window) and then call method to clean data. Thing is that, i want to logout all buttons by changing their flag in base (isLogged from 1 to 0) so in OnExit im calling this method: LogoutUser.logoutMe(); but its not cleaning data, when someone use alt-f4 to close application ;) – user13657 Oct 02 '12 at 13:29
  • * all users, not all buttons... ;) – user13657 Oct 02 '12 at 13:29
  • You need to debug that method - put a breakpoint on it. Is it being called correctly? – Dan Puzey Oct 02 '12 at 13:43
  • Hmm, have no idea why, but in some windows it works (logout), but on some other doesnt.. – user13657 Oct 02 '12 at 13:48