hardcodet / wpf-notifyicon

NotifyIcon (aka system tray icon or taskbar icon) for the WPF platform
MIT License
830 stars 130 forks source link

question for external service shutdown #27

Closed HobieHodge closed 4 years ago

HobieHodge commented 4 years ago

On exiting a service normally, the tray icon will automatically be removed from the system tray, which is good. But I have an external app that auto updates the service by locating the process, performing process.CloseMainwindow() then process.Kill() prior to updating and restarting the service: in this scenario the old icon remains in the system tray until you hover over it, so you will briefly see two icons. Is there some cleaner way in this scenario you could suggest to remove the icon for the old instance from the system tray?

Lakritzator commented 4 years ago

I forgot to answer this, when I saw it pass by...

If you would kill the process from outside, there is no way of preventing this.

But as you are closing the main window first, you should simply hide the icon in the closing event of the main window and it's solved.

HobieHodge commented 4 years ago

I tried that but Window_Closing event on MainWindow is never raised when CloseMainWindow() is called, I'm guessing because the window is hidden to begin with? I tried pausing and inspecting, the process properties in debug: it should theoretically exit gracefully after calling CloseMainWindow() given a little time, without any need to use Kill(), but it doesn't. It is not a big deal though, just an internal utility used by a few people and otherwise works OK. I may play around with it later, there is probably something I'm missing.

Lakritzator commented 4 years ago

There must be a mistake in the application, process.CloseMainwindow sends a close message to the main window of that application which should allow the application to gracefully close. Maybe the even handlers were not correctly setup? Maybe you are right that if the windows is hidden it doesn't process the message, although that sounds unlikely.

You might also try different ways of communicating with the other process. Anyway, from my point of view it's not a discussion which belongs here in this repository, I will close the issue.