I’m having a problem with an application that seems to cancel the shutdown or restart of my computer.
I think it’s because I capture the form closing event and cancel it like this:
private void Main_FormClosing(object sender, FormClosingEventArgs e)
{
if (!AllowApplicationClose)
{
e.Cancel = true;
this.WindowState = FormWindowState.Minimized;
}
}
I do this so that the close button just minimizes to the task bar. I do want to be able to restart without exiting the application though. Is there a better way to do this? Or a way to know if windows is closing the application?
EDIT: Thanks for the answers. Sorry I could only pick one as correct, I just went with the top one. Thanks again!
You can use
CloseReasonto detect if the form is being closed by the user or better (in your case) use theSystemEvents-class to get an event when the system is trying to log off or shutdown/restart so you can also close the application when it is minimized.