|
-
Aug 6th, 2006, 02:37 AM
#1
Thread Starter
Lively Member
How Can I Tell If Windows Is Shutting Down
How can i tell if windows(operating system) is being shut down?
Thanks in advance.
-
Aug 6th, 2006, 02:47 AM
#2
Thread Starter
Lively Member
Re: How Can I Tell If Windows Is Shutting Down
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();
}
-
Aug 6th, 2006, 03:19 AM
#3
Re: How Can I Tell If Windows Is Shutting Down
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();
}
-
Aug 7th, 2006, 02:26 AM
#4
Thread Starter
Lively Member
Re: How Can I Tell If Windows Is Shutting Down
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?
-
Aug 7th, 2006, 02:51 AM
#5
Re: How Can I Tell If Windows Is Shutting Down
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|