do some code before shutdown windows

92 Views Asked by At

I have program , start when windows start. Users have to open this app and they can’t close it. When users open it, a variable in sql will be true, but I want before shutdown windows this variable will be false. How this possible? Users can’t close the app: e.Cancel = true; but I want before shutdown , variable in database will be false. thanks

1

There are 1 best solutions below

1
On

You should catch event on windows shutting down. And in handler you can exec sql query, that change your field in table.

Here you can get more information about windows shutting down: How to detect Windows shutdown or logoff

EDIT

You can use this code in handler of FormClosing event:

if (e.CloseReason == CloseReason.WindowsShutDown)
{
    // do some sql stuff
}
else
{
    e.Cancel = true;
}

When Windows is shutting down it send close signal to all applications.