Keep in mind, this is a Console App running .NET with C#. Also, I might later convert this into a service, but for now, I need to handle the shutdown while my Console App is running.
My system sometimes gets shutdown automagically by Windows Updates or by Kace Updates. This is irritating when my program is in the middle of doing long tests in the middle of the night and I'm not there to see it happen. I would like to be able to detect the system shutting down and delay it long enough for my tests to finish gracefully and allow me to gracefully exit my program keeping all my test data intact.
I've looked at various threads and can't seem to find a working solution. I've tried both of these:
AppDomain.CurrentDomain.ProcessExit += CurrentDomain_ProcessExit;
Application.ApplicationExit += OnApplicationExit;
But neither of these events seem to get fired. I basically want to send an e-mail notification to myself when the shutdown is detected and pause the shutdown. Or at least log a message somehow...
I can simulate this behavior using (of all things) 'notepad.exe
'. I just open notepad.exe, type some junk, and then attempt to ShutDown my computer. The computer tries to shutdown, and then shows a message that says it cannot shut down because 'notepad' is blocking it.
I would like MY program to block the shutdown (or at least delay it) the same way as 'notepad.exe' does it.
Any information, events, or other ideas that you may have would be very useful. Thanks in advance for your assistance.