469,927 Members | 1,863 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,927 developers. It's quick & easy.

Detecting/trapping the shutdiwn event

Some time ago I tracked down the code for detecting the shutdown event
within a C# program. I tested it, it worked, and I moved on. However, I just
discovered that the code is no longer working, and I'm baffled. I've done
some more Google searches and have come up with the same thing. A portion of
the code is below. I call the HookSessionEnding routine in the Load event of
the main form, but the shutdown event code never fires. I'm baffled.

using Microsoft.Win32;

private void HookSessionEnding()
{
SystemEvents.SessionEnding += new
SessionEndingEventHandler(this.OnSessionEnding);
}

private void OnSessionEnding(object sender, SessionEndingEventArgs e )
{
MessageBox.Show("Shutting down");
}
Nov 17 '05 #1
2 2154

"Paul Steele" <pa*********@acadiau.ca> wrote in message
news:OR**************@TK2MSFTNGP10.phx.gbl...
Some time ago I tracked down the code for detecting the shutdown event
within a C# program. I tested it, it worked, and I moved on. However, I
just discovered that the code is no longer working, and I'm baffled. I've
done some more Google searches and have come up with the same thing. A
portion of the code is below. I call the HookSessionEnding routine in the
Load event of the main form, but the shutdown event code never fires. I'm
baffled.

using Microsoft.Win32;

private void HookSessionEnding()
{
SystemEvents.SessionEnding += new
SessionEndingEventHandler(this.OnSessionEnding);
}

private void OnSessionEnding(object sender, SessionEndingEventArgs e )
{
MessageBox.Show("Shutting down");
}


I've done some more Googling and have found part of my problem. I had a form
that sets e.Cancel to true and hides the form instead of actually closing
it. This works fine in normal operation but the e.cancel actually cancels
the shutdown event. Using the technique above the Window Closing event fires
before the OnSessionEnding event so it looks the same as a normal close
event. To get around the problem there's another way to trap SystemShutdown
using this code:

protected override void WndProc(ref System.Windows.Forms.Message m)
{
if (m.Msg == 0x11)
{
//do whatever shutdown code you want to do
systemShutdown = true; //Use this in the Windows Closing event.
}
base.WndProc(ref m); //continue the regular shutdown process
}

Unfortunately even with this code the Windows Closing event *still* fires
first. This is the article that implies that it shouldn't:

http://msdn.microsoft.com/library/de...ndingtopic.asp

Has anyone else done this sort of thing successfully?
Nov 17 '05 #2
Solved my own problem. The WndPrc override function has to go in each form
with special close operation...

"Paul Steele" <pa*********@acadiau.ca> wrote in message
news:e8*************@TK2MSFTNGP15.phx.gbl...

"Paul Steele" <pa*********@acadiau.ca> wrote in message
news:OR**************@TK2MSFTNGP10.phx.gbl...
Some time ago I tracked down the code for detecting the shutdown event
within a C# program. I tested it, it worked, and I moved on. However, I
just discovered that the code is no longer working, and I'm baffled. I've
done some more Google searches and have come up with the same thing. A
portion of the code is below. I call the HookSessionEnding routine in the
Load event of the main form, but the shutdown event code never fires. I'm
baffled.

using Microsoft.Win32;

private void HookSessionEnding()
{
SystemEvents.SessionEnding += new
SessionEndingEventHandler(this.OnSessionEnding);
}

private void OnSessionEnding(object sender, SessionEndingEventArgs e )
{
MessageBox.Show("Shutting down");
}


I've done some more Googling and have found part of my problem. I had a
form that sets e.Cancel to true and hides the form instead of actually
closing it. This works fine in normal operation but the e.cancel actually
cancels the shutdown event. Using the technique above the Window Closing
event fires before the OnSessionEnding event so it looks the same as a
normal close event. To get around the problem there's another way to trap
SystemShutdown using this code:

protected override void WndProc(ref System.Windows.Forms.Message m)
{
if (m.Msg == 0x11)
{
//do whatever shutdown code you want to do
systemShutdown = true; //Use this in the Windows Closing event.
}
base.WndProc(ref m); //continue the regular shutdown process
}

Unfortunately even with this code the Windows Closing event *still* fires
first. This is the article that implies that it shouldn't:

http://msdn.microsoft.com/library/de...ndingtopic.asp

Has anyone else done this sort of thing successfully?

Nov 17 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Erik Bethke | last post: by
13 posts views Thread by Thelma Lubkin | last post: by
1 post views Thread by Chirag Malvi | last post: by
2 posts views Thread by Captain Nemo | last post: by
15 posts views Thread by RobG | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.