Can't Shutdown with System Tray Application Running

  • Thread starter Michael Hetrick
  • Start date
M

Michael Hetrick

I have created a System Tray application that works as expected until the
user tries to shutdown. The computer will not shutdown because the System
Tray application is still running.

Can someone please provide an example of how to close the application
automatically if the user chooses to shutdown without the user explicitly
closing the application itself?

Thanks -
Michael Hetrick
 
M

Michael Hetrick

That's not exactly it. I have it running in the System
Tray, but when shut down won't happen.

Is SystemEvents.SessionEnded the way to go here? If it
is, I'm not sure how to use it.

Michael
 
Y

Ying-Shen Yu[MSFT]

Hi Michael,

Thanks for your post!

To my understading now, your app works fine except it will prevent the
system shutdown?

In my quick test, a simple winform with a trayicon does not have this
problem, so probably the problem lies in some where else. you may remove
the trayicon from your app and try shutting down again.
Does this problem still exists?

I'd like to know what's the behavior of the system if you shutdown with
your app running?
Will it show a progress dialog with title "Ending ..." or just nothing
special happened, the shutdown process just stopped without any prompt?

If you saw the "Ending..." dialog, probably the main thread of your app was
busy working( or blocked) and do not have chance to process the system
message.

If you see nothing prompt, then have you handled the Form.Closing event or
Form.Closed event?
If yes, could you show me the code snippet of these two event handler? If
you didn't handle these events, I'd like to get simple repro sample
project, and I will do some research on it and see if I can figure out the
problem.

Thanks!

Best regards,

Ying-Shen Yu [MSFT]
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security

This posting is provided "AS IS" with no warranties and confers no rights.
This mail should not be replied directly, "online" should be removed before
sending.
 
M

MikeP

I know. It is the same problem I was having. When you seperate out the form
that handles the Tray Icon from the form that handles your UI, the problem
goes away. I am assuming thay you have changed the cancel [X] to minimize
the form instead of close the app. This is the same message windows sends to
your app when it wants to close and you have programmed your application to
ignore it.

You can try to catch that system messages if you like. I think you will find
my solution cleaner and much easier.
 
M

Michael Hetrick

This is what I did, and it seems to work, but I don't know why:
public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
this.ShowInTaskbar = false;
SystemEvents.SessionEnded += new
SessionEndedEventHandler(SystemEvents_SessionEnded);
}

private static void SystemEvents_SessionEnded(object sender,
SessionEndedEventArgs e)
{
Application.Exit();
}
 
Y

Ying-Shen Yu[MSFT]

Hi Michael,

According to documentation, Application.Exit will not raise Form.Closed or
Form.Closing events.

If this way works, I suspect the problem might lie in the event handler of
these events. To verify this, we can try adding Form.Close method before
Application.Exit in the SessionEnded handler.

If the problem could be reproed, you may override the OnClosed method and
OnClosing method of your mainform and add some debug code like this:
<code>
protected override void OnClosing(CancelEventArgs e)
{
if (0 == Trace.Listeners.Count) {
Trace.Listeners.Add(new TextWriterTraceListener("debugoutput"));
}

Trace.WriteLine ("OnClosing Enter");
base.OnClosing (e);
Trace.WriteLine (e.Cancel);
Trace.WriteLine ("OnClosing Exit");
}
</code>

Maybe we can find some clue from the debug output.
Thanks!



Best regards,

Ying-Shen Yu [MSFT]
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security

This posting is provided "AS IS" with no warranties and confers no rights.
This mail should not be replied directly, "online" should be removed before
sending.
 

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