I think each of us witnessed Windows explorer crash. Taskbar disappears but it reloads after this. Unfortunatelly not all tray icons appear reloading. It depends on the software using tray icon.
I've been playing with tray icons and it works well great until explorer crash happened. My appplication runs in the background but I'm not able to show its window beacause tray icon is gone. The only thing I can do it to kill it by task manager without saving progress.
I'm using Windows XP and tried to simulate this crash by killing explorer.exe process through task manager CTRL+ALT+DEL and reloading it by File --> New Task --> "explorer" --> OK
After this I'm not able to add icon again because it says this icon is still there. Indeed it is because I'm able to get this icon though I can't see it.
Any ideas how can protect my application and how can I detect that my icon disappeared after explorer crash?
It would be stupid to schedule tray.remove(...) and tray.add(...) just to show possible "hidden" icons.
If I understand the blog correctly, there is a message sent by Windows (the OS) to all existing windows whenever the Explorer starts and creates a new taskbar. To receive these messages, you need a window handle. Though the JVM undoubtedly creates some windows to integrate running application into the OS, you probably won't be able to get handles of these windows, nor process the messages OS sends to them.
Assuming there is no native way to handle this from Java (which I honestly don't know), a possible way would be to program a DLL and use it to create a hidden window at application startup. This window would process the WM_TASKBARCREATED message. Even though a callback from DLL to Java is probably possible, I personally would just set a flag in the DLL upon receiving this message and use a timer to poll this flag time from time (say, once per second). If the Explorer just crashed, waiting a second for the icon to get resurrected is not a bad deal. Also, before the application exits, the DLL's window should be destroyed. Some thoughts should be also given to the possibility of multiple clients of the DLL (one DLL window could serve multiple JVMs), but we're getting quite stretched already.
It's a looooong time since I was doing DLLs and Windows development in C, so I can't go into greater detail here, but in my opinion this should be viable. However, you do need some experience with DLLs and JNA/JNI in Java.