I am developing an app that need to save the time when computer shutdown, I need to be sure that always computer shutdown/restart, this code executes.
I'm writing it at Form Closing event like this:
private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
{
if(e.CloseReason == CloseReason.WindowsShutDown)
{
Properties.Settings.Default.lastShutdown = DateTime.Now;
Properties.Settings.Default.Save();
}
}
Usually the code is working and it's saving the DateTime in settings, but I think sometimes the app is closed before the saving is done and lost the data. I don´t know how much time has the app before closing when shutdown.
¿Is there any way I can be sure that code always finish before shutdown?
Thanks.