tags:

views:

27

answers:

1

I have a Windows form application which exit to a system tray Notifyicon and I have tried calling Application.Exit() or Me.Close() to close the program. But neither works to close the program. What is the correct way to close it?

+3  A: 

' It's a good idea to make the system tray icon invisible before ending ' the application, otherwise, it can linger in the tray when the application ' is no longer running.

That bit is referring to the notification area (system tray) not really polling to make sure the handles corresponding to the icons are still around. So if something doesn't exit gracefully or otherwise doesn't clean up its notification icon, then the notification area doesn't realize that that window handle flew the coop.

You've probably seen this behavior before: sometimes you mouse over the notification area and as soon as the mouse begins to hover over an icon to an application you know you quit some time ago, the notification area goes "Well shut my mouth and paint me red, guess that handle is no longer around" and poof! removes the icon.

But after I close the program, the tray icon is still showing and all functions are available. It seems to me the program is still running.

Yeah, it would seem to me your application is still running. Note that Application.Exit() doesn't have to succeed--there is an overload that allows other parts of the application to cancel the request. So that's something to check.

The notify icon isn't really going to keep your app "alive" as it doesn't have a message pump under your control, as it were. Something in your app is not exiting.

Hope that helps get you on the right track!

Nicholas Piasecki
Hi buddy, I now realised what is wrong. I implemented "exit to system tray" like this:in the formclosing event handler, set e.Cancel= true, hide the winform and show the notifyicon. Since I cancel the form close, future form close will also be cancelled, that is the problem. What I do now is adding a global Boolean varaible RealExit = False. On exiting from trayicon, set the Boolean to be True. And only cancel the form close event when Boolean variable is False. It solved the problem. What is your comment on this practice? I there a proper way to "exit to system tray and then exit"?
Aperture
That is reasonable. Also in the Form Closing event arguments there is a CloseReason property. You could do if (e.CloseReason == CloseReason.ApplicationExitCall) and you'd be all set.
Nicholas Piasecki