I have a WPF Control (e.g. Button) that closes the application. I wonder are there any differences between a custom built event handler to close the application and the default closing event:
Basically are there difference between the following two?:
private void Btn_Click(object sender, RoutedEventArgs e)
{
RibbonWindow.Close();
}
and
protected override void OnClosed(EventArgs e)
{
base.OnClosed(e);
}
Difference between custom event handler (
Btn_Click) for closing app and default event (OnClosed)is that even if you close the app with custom event default will always be called at the end. That is not the case with default. Once default is called custom won’t be called after that because obviously button is not clicked. In short, default event should be handled when you must need an event when application is closing (even after close button is clicked) while you can use custom event to ask userare you sure?kind of thingFor example let’s say you create a temp file during application lifetime and you must have to delete before app exists.
Now you can put this code inside custom button click event but what if user closes the app with
Alt + F4or by any way other than your button? In that case your button code will not be called but default close event will always be called and you can safely delete the temp file.