WinBatch® Technical Support Forum

All Things WinBatch => WinBatch => Topic started by: gibberish on July 05, 2019, 08:45:46 AM

Title: IntControl 1007 - Tray icon randomly disappears from systray
Post by: gibberish on July 05, 2019, 08:45:46 AM
I have a swiss-army-knife desktop app that runs in the background and monitors a bunch of stuff, lets me show/hide windows, etc.

I use IntControl 1007 to display a tray icon that controls the app, using MegaPop (many thanks to IFICantBYTE - it works brilliantly!) to provide a pop-up menu from the tray icon.

The tray icon should always be present if the app is running, only disappearing when told to do so on app close.

However, from time to time the tray icon just disappears (it is not being moved to the "hidden icons" in the systray, it is truly gone).

It would be great to have a p1 value for IntControl 1007 that queries the tray icon's status (present/absent) and allows me to only refresh the tray icon if it is missing. Without that, though, are there any thoughts on how to best manage the situation?

I've just now added a call in the app's main loop that re-writes the tooltip each loop, but I won't know for several weeks (or months) if that "did the trick" or not.

I guess my question is: if the tray icon has disappeared and I refresh the tooltip, will that cause the tray icon to reappear in the systray?  (I realize this might be a "nobody knows" situation since this is a rather esoteric question, but am asking in case someone has encountered it before).

Or does anyone else have a better idea re how to solve this situation?  Is there maybe a DLL call that can be made to check the state of the tray icon?

I am using WB 2018 B.
Title: Re: IntControl 1007 - Tray icon randomly disappears from systray
Post by: td on July 05, 2019, 09:33:03 AM
You should have also mentioned your Windows version, e.g., Windows 10 1809. 

Often, when a non-windows provided system tray Icon disappears it is because the Windows Shell has silently crashed and restarted.  If that is the case, you will need to figure out what is causing your shell to occasionally crash.  That can be difficult because the OS automatically restarts the shell after a crash and it can all happen in an imperceptible instant.
Title: Re: IntControl 1007 - Tray icon randomly disappears from systray
Post by: gibberish on July 05, 2019, 10:01:40 AM
Ahhhh... very useful to know.  I also note that occasionally Google Chrome instances will disappear from the taskbar - I need to cycle explorer.exe and they re-appear. Perhaps a mini OS crash is what is causing both problems. Still,  after cycling explorer.exe I've never seen a disappeared tray icon re-appear, and never observed any connection between a missing trayicon and missing Google Chrome taskbar instances.

Any further thoughts about querying the visibility of the tray icon?

I am using Win7 HP.
Title: Re: IntControl 1007 - Tray icon randomly disappears from systray
Post by: td on July 05, 2019, 10:13:29 AM
The behavior is a function of the way the Windows Shell is "designed" to work on your version of Windows.  On Windows 10 1903 you simply need to install a shortcut for your script/application in the shell's startup directory and it will usually reconnect to the system tray when Explorer restarts.  However, I am not sure that this is the case on Windows 7.  (How quickly I forget the details of old versions of Windows...)

Anyway,  you can find the shell's startup location by simply calling dirStartup =ShortCutDir("Common Startup", 1,1) to get the startup folder for all users. On Windows 10 this is normally the "C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Startup\" directory on the file system.  Again, not sure if this will solve the problem on Windows 7.
Title: Re: IntControl 1007 - Tray icon randomly disappears from systray
Post by: gibberish on July 05, 2019, 10:42:44 AM
Yes, I already am starting the app from a shortcut stored in C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Startup

Either Win7 is not automatically re-connnecting the icon to the system tray, or the problem is summat else.

I'll wait and see if refreshing the tooltip associated to the tray icon does the trick, and if not, I'll try the more heavy-handed method of removing/reinstalling the tray icon with each loop. I suspect, though, that the regular disappear/reappear of the tray ICON will be more annoying that my current workaround of manually dropping a trigger file into the app folder whenever I see the tray icon is missing.
Title: Re: IntControl 1007 - Tray icon randomly disappears from systray
Post by: gibberish on July 05, 2019, 11:21:10 AM
Is it possible that this below article might be the answer to discovering if, at any time, the trayicon has vamooshed?

https://docs.microsoft.com/en-us/dotnet/api/system.windows.forms.notifyicon.visible

The C languages are greek to me (I am only conversant with javascript, python and WIL), so will request a few pointers before attempting to tackle (perhaps links to a comparable WBT code example or two to follow?)

Many thanks
Title: Re: IntControl 1007 - Tray icon randomly disappears from systray
Post by: td on July 05, 2019, 02:35:24 PM
The article is about using .Net Framework Class Library classes to place a Windows Forms application on the system tray using C#.  It really isn't applicable to your problem and the C# language is definitely not the C programming language.

Instead of fixating on your original notion of how to fix your problem you might want to consider other solutions.   
Title: Re: IntControl 1007 - Tray icon randomly disappears from systray
Post by: cssyphus on September 11, 2019, 09:16:18 AM
Here's how I solved this.

IntControl(1007) returns a value whenever the tray tooltip is modified.

Add the below InitVars sub to your startup code:


:InitVars
str_currTrayToolTip = "Initial trayicon tooltip goes here"
str_lastTrayRefresh = timeYmdHms()
c_TrayIconFile = ""
c_ModifyTrayIcon = 4
c_ModifyTrayTooltip = 1
c_HideWBTaskbarIcon = 1
Return ;InitVars


Also add a sub like the below CheckTrayIcon to your main program loop.


:CheckTrayIcon
IF timeDiffSecs(timeYmdHms(), str_lastTrayRefresh) > 4
str_lastTrayRefresh = timeYmdHms()
_tmp = intControl(1007, c_ModifyTrayIcon, c_ModifyTrayTooltip, str_currTrayToolTip, c_TrayIconFile)
while _tmp == 0
timeDelay(0.1)
_tmp = intControl(1007, c_RemoveFromTray,0,0,0)
timeDelay(0.1)
_tmp = intControl(1007, c_AddToTray, c_HideWBTaskbarIcon, str_currTrayToolTip, c_TrayIconFile)
timeDelay(0.2)
endwhile
ENDIF
Return ;CheckTrayIcon


Every 4 seconds sub CheckTrayIcon will refresh the trayicon tooltip text and if it fails (indicating the trayicon has "vamooshed") it will reload the trayicon.
Title: Re: IntControl 1007 - Tray icon randomly disappears from systray
Post by: td on September 12, 2019, 08:28:51 AM
Good suggestion.  It should work provided the process is simply detached rather than terminated.