user998609
user998609

Reputation: 21

Detect Windows shutdown with C#

I want to stop my C# application from running after the user has clicked shut down of computer so any idea how to do it?

Upvotes: 1

Views: 992

Answers (1)

you can use CloseReason.WindowsShutDown for this purpose inside the form closing event.

if (e.CloseReason != CloseReason.WindowsShutDown)
{
    e.Cancel = true;
}
else
{
    e.Cancel = false;
}

Upvotes: 8

Related Questions