How can i tell if windows(operating system) is being shut down?
Thanks in advance.
Printable View
How can i tell if windows(operating system) is being shut down?
Thanks in advance.
i found this code but I have no clue where to put it
or if it even works
Code:
private void SystemEvents_SessionEnded(object sender,SessionEndedEventArgs e)
{
bOkToExit = true;
Application.Exit();
}
I've never seen that before myself, but I assume that it is just like any other event handler because it looks like any other event handler. From the name, that means that it's handling the SessionEnded event of the SystemEvents. I simply looked that up in the Help to find out that it's the Microsoft.Win32.SystemEvents class and the SessionEnded event is static. You therefore simply add the appropriate delegate just like you do for any other event handler:Code:public Form1()
{
InitializeComponent();
Microsoft.Win32.SystemEvents.SessionEnded += new Microsoft.Win32.SessionEndedEventHandler(SystemEvents_SessionEnded);
}
private void SystemEvents_SessionEnded(object sender, Microsoft.Win32.SessionEndedEventArgs e)
{
// As the documentation clearly states, you must detach the event handler or a memory leak will occur.
// I guess this isn't too important if the system is shutting down but it's still the right thing to do.
Microsoft.Win32.SystemEvents.SessionEnded -= new Microsoft.Win32.SessionEndedEventHandler(SystemEvents_SessionEnded);
bOkToExit = true;
Application.Exit();
}
thanks but the code i posted up does not execute before windows shuts down.
My program hangs around in the system tray and when the user hits the close button the program actually sets the visable property to false so the program is still running.
The problem I run into is that windows hangs when you try to shut down the computer.
Any ideas on how to fix this?
Have you tried handling the SessionEnding event rather than SessionEnded? The most common way I've seen this done is to override the WndProc method and trap the message that is received when Windows is shutting down. I can't recall what message it is but I'm sure it's been posted before.