Re: Handling System Shutdown in a WindowsForms App by Stoitcho
Stoitcho
Tue Dec 06 12:08:21 CST 2005
Alex,
The error is that you need to use *ref* infront of the *m* parameter when
calling the base methods.
This is the code that you can use to test the sample. I changed also the
event handler to override OnClosing methods, which is the preferable way of
handling events in a case of inheritance.
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(ref m);
} //WndProc
protected override void OnClosing(CancelEventArgs e)
{
base.OnClosing(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;
}
}
}
--
Stoitcho Goutsev (100) [C# MVP]
"Alex Maghen" <AlexMaghen@newsgroup.nospam> wrote in message
news:545DDB52-C39A-442A-86B0-7B357DAD7A96@microsoft.com...
> 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" wrote:
>
>> 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) [C# MVP]" wrote:
>>
>> > 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]
>> >
>> >
>> > "Alex Maghen" <AlexMaghen@newsgroup.nospam> wrote in message
>> > news:EAD31A6E-F3E7-4E4D-B77B-72FE7FD4E89A@microsoft.com...
>> > > 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
>> >
>> >
>> >