Handling System Shutdown in a WindowsForms App

G

Guest

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
 
S

Stoitcho Goutsev \(100\) [C# MVP]

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.
 
G

Guest

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]


Alex Maghen said:
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
 
G

Guest

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]


Alex Maghen said:
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
 
S

Stoitcho Goutsev \(100\) [C# MVP]

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 said:
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
 
J

Jeffrey Tan[MSFT]

Hi Alex,

Yes, it seems that the MSDN sample missed "ref " in base.WndProc(m)
statement.
The correct code is:
base.WndProc(ref m), which has been pointed out by "Stoitcho Goutsev
\(100\) [C# MVP]".

Thanks

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
J

Jeffrey Tan[MSFT]

Hi Alex,

Does our replies make sense to you? Is your problem resolved? Please feel
free to tell me, thanks

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top