By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,574 Members | 2,966 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,574 IT Pros & Developers. It's quick & easy.

Terminating Systray Application On Shutdown does not work (it does not exit the app)

P: n/a
I have written an application that lives in the systray but when I try
to close windows or logout it does not close my application and
therefore prevents windows from shutting down or logging me out. It is
essential that I solve this problem but I have run out of ideas can
anyone help (please!)

The application starts in Start.cs which kicks off the Systray form
using this call:
Application.Run(new Systray());

Systray is a mimized form that is not visible in the TaskBar but has a
NotifyIcon which sits in the systray. Associate with the icon is a
context menu with a Terminate command in it i.e. Application.Exit();
and that works fine. Presumably windows must send some sort of
terminate message to all running applications and I need to capture
that? Can I use one of the standard events or do I need to use the
windows WndProc?

Any suggestions of where to look would be greatly appreciated.

Regards, Jon
Nov 15 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Max
The general idea is that your application's main window should handle
WM_QUERYSHUTDOWN and WM_SHUTDOWN correctly.

Correctly means when you receive WM_QUERYSHUTDOWN you should answer whether
your are ready for it or wish to cancel the shutdown because of some unsaved
work, etc.
When you receive WM_SHUTDOWN - you should cancel.

Your application answers "yes" on query shutdown, but does not exit.

I do not know exactly how event handling for those events is implemented in
Windows.Forms. If you will not find out - you will need to overload windows
procedure (WndProc function).

"Jon Bosker" <Go*******@VisualBasic.net> wrote in message
news:b1**************************@posting.google.c om...
I have written an application that lives in the systray but when I try
to close windows or logout it does not close my application and
therefore prevents windows from shutting down or logging me out. It is
essential that I solve this problem but I have run out of ideas can
anyone help (please!)

The application starts in Start.cs which kicks off the Systray form
using this call:
Application.Run(new Systray());

Systray is a mimized form that is not visible in the TaskBar but has a
NotifyIcon which sits in the systray. Associate with the icon is a
context menu with a Terminate command in it i.e. Application.Exit();
and that works fine. Presumably windows must send some sort of
terminate message to all running applications and I need to capture
that? Can I use one of the standard events or do I need to use the
windows WndProc?

Any suggestions of where to look would be greatly appreciated.

Regards, Jon

Nov 15 '05 #2

P: n/a
Thanks Max - you have certainly helped me on my way. With that info I
found some more discussion and here is what I have got:
protected override void WndProc(ref Message m)
{
switch (m.Msg)
{
case 0x11: // WM_QUERYENDSESSION
TerminateApp(false);
break;

case 0x12: // WM_QUIT
TerminateApp(false);
break;

case 0x16: // WM_ENDSESSION
TerminateApp(false);
break;
}
base.WndProc(ref m);
}

My class TerminateApp then either asks the user if they want to
terminate (if true is passed as a parameter) and if not closes up
without question. This works in as much as the application is now
terminated when the user tries to shut down Windows but Windows still
does not close. The user has to tell Windows to shutdown a 2nd time
and this time it works because my application is gone. So.... can
anybody tell me why.... somehow I think I have to return a different
value to Windows (I think I need to return a 1?) but I cannot see how
to do that.

Regards, Jon
Nov 15 '05 #3

P: n/a
Max
See
http://msdn.microsoft.com/library/de...endsession.asp

It says you should return TRUE if you agree to cancel.
To the best of my knowledge it means that you you should set m.Result=1;

You do not need to actually close the window or application on Query End
Session. You should just decide if you are ready to close or not and respond
accordingly.

You will recieve WM_ENDSESSION. WM_ENDSESSION is a proper place to close.

See also SystemEvents.SessionEnding Event topic in Framework SDK
documentation to learn about the ".NET way" to handle it.

"Jon Bosker" <Jo*******@Yahoo.com> wrote in message
news:2f**************************@posting.google.c om...
Thanks Max - you have certainly helped me on my way. With that info I
found some more discussion and here is what I have got:
protected override void WndProc(ref Message m)
{
switch (m.Msg)
{
case 0x11: // WM_QUERYENDSESSION
TerminateApp(false);
break;

case 0x12: // WM_QUIT
TerminateApp(false);
break;

case 0x16: // WM_ENDSESSION
TerminateApp(false);
break;
}
base.WndProc(ref m);
}

My class TerminateApp then either asks the user if they want to
terminate (if true is passed as a parameter) and if not closes up
without question. This works in as much as the application is now
terminated when the user tries to shut down Windows but Windows still
does not close. The user has to tell Windows to shutdown a 2nd time
and this time it works because my application is gone. So.... can
anybody tell me why.... somehow I think I have to return a different
value to Windows (I think I need to return a 1?) but I cannot see how
to do that.

Regards, Jon

Nov 15 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.