More information...
For testing purposes, I've tried creating a simple WindowsForms app in VS
2005 and the copying and pasting the sample code from
"SystemEvents.SessionEnding Event" in the help into the Form1 class. That
sample code is:
private static int WM_QUERYENDSESSION = 0x11;
private static bool systemShutdown = false;
protected override void WndProc(ref System.Windows.Forms.Message m)
{
if (m.Msg==WM_QUERYENDSESSION)
{
MessageBox.Show("queryendsession: this is a logoff, shutdown, or
reboot");
systemShutdown = true;
}
// If this is WM_QUERYENDSESSION, the closing event should be
// raised in the base WndProc.
base.WndProc(m);
} //WndProc
private void Form1_Closing(
System.Object sender,
System.ComponentModel.CancelEventArgs e)
{
if (systemShutdown)
// Reset the variable because the user might cancel the
// shutdown.
{
systemShutdown = false;
if (DialogResult.Yes==MessageBox.Show("My application",
"Do you want to save your work before logging off?",
MessageBoxButtons.YesNo))
{
e.Cancel = true;
}
else
{
e.Cancel = false;
}
}
}
When I try to run the app, I get an error about the "base.WndProc(m);"
line
above. What am I doing wrong?
Alex
Alex Maghen said:
Forgive my ignorance about this but I'm even a little confused about how
I
create a function in my Main Form class and make it handle this event.
Can
you give me a small snippet of code or point me somewhere?
Alex
Stoitcho Goutsev (100) said:
Alex,
Try to handle SessionEnding event first and make sure you don't cancel
the
request.
Read carefully MSDN regarding this event because they have some
important
remarks there.
--
HTH
Stoitcho Goutsev (100) [C# MVP]
Hi. I have an application that runs as a Tray Icon app with no
visible
window
(most of the time) and right now, the system will not shut down while
that
app is running. I think I need to handle "SystemEvents.SessionEnded"
or
something to properly deal with that but I don't understand how,
inside
the
code for my Main Form for the application, I set up a handler for the
event
and then what I actually DO with the event once it comes (though I
assume
I
just do whatever I would normally do to exit my application
grcefully).
Can someone help me with a sample?
Alex