Jump to content

Tray Icon Disappearing Problem


Recommended Posts

I have an AutoIt script running on a few thousand computers that basically just sits in the system tray and allows support staff to ask a user to run a few common tools. What I've noticed recently is that sometime the AutoIt icon is missing from the tray entirely, but a check at Task Manager shows that the process is still running. The icon is not in the hidden area of the System Tray, it's just ... gone. Killing the task and restarting the app, or logging out and back in brings the icon back. These users would not know how to get into the debugging mode of the script (double right click / CTRL+F2) that I put in there so I wouldn't think that's causing any issue. Is there any AutoIt / Windows timeout that's occurring causing the icon to go away but the process remains?

The code is below. Not quite a work of art, but it's been working so far :-)

#Include <Constants.au3>
#Include <misc.au3>

$title = "Desktop Tools"
$flagError = 48
$flagInfo = 64

$mnuQuit = "X"
$mnuVersion = "X"
$mnuDebugSep = "X"

$appCScript = @WindowsDir & "\system32\cscript.exe"

Opt("TrayIconDebug", 0)
Opt("TrayAutoPause", 0)
Opt("TrayMenuMode", 1)

if _Singleton("DesktopTools", 1) = 0 then
    Msgbox($flagError, $title, "This tool is already running.", 15)
    exit(0)
EndIf

TraySetToolTip("Desktop Tools")

$mnuMapDrives = TrayCreateItem("Map Network Drives")

TrayCreateItem("")

$mnuIPAddress = TrayCreateItem("What is my IP Address?")
$mnuNetBIOSName = TrayCreateItem("What is my Computer Name?")

While 1
    $msg = TrayGetMsg()
    Select
        Case $msg = 0
            ContinueLoop
        Case $msg = $mnuMapDrives
            If Not FileExists("\\domain\netlogon\loginscript.vbs") then 
                msgbox($flagError, $title, "Error Mapping Network Drives: Cannot Find Login Script.  Ensure that you are connected to the Network.")
            ElseIf Not FileExists($appCScript) then
                msgbox($flagError, $title, "Error Mapping Network Drives: Cannot Find CSCRIPT.  Please call the Support Center.")
            Else
                SplashTextOn("", "Please Wait While Your Drives Are Mapped.", 450, 30, 40, 40, 1 + 32, "Arial", 12, 400)
                $runLoginScript = ShellExecuteWait($appCScript, "//nologo \\domain\netlogon\loginscript.vbs")
                SplashOff()
                if $runLoginScript <> 0 Then
                    msgbox($flagError, $title, "Login Script Returned Error " & $runLoginScript & ".  Please call the Support Center.")
                Else
                    msgbox($flagInfo, $title, "Your drives have been mapped.")
                EndIf
            EndIf
        Case $msg = $mnuIPAddress
            $ipList = ""
            if ValidIP(@IPAddress1) then $ipList = $ipList & @IPAddress1 & @CRLF
            if ValidIP(@IPAddress2) then $ipList = $ipList & @IPAddress2 & @CRLF
            if ValidIP(@IPAddress3) then $ipList = $ipList & @IPAddress3 & @CRLF
            if ValidIP(@IPAddress4) then $ipList = $ipList & @IPAddress4 & @CRLF
            if $ipList <> "" Then
                msgbox($flagInfo, $title, "Your IP Address is: " & @CRLF & @CRLF & $ipList)
            Else
                msgbox($flagError, $title, "Unable to determine your IP Address.")
            endif
        Case $msg = $mnuNetBIOSName
            msgbox($flagInfo, $title, "Your Computer Name is: " & @CRLF & @CRLF & @ComputerName)
        Case $msg = $mnuVersion
            msgbox($flagInfo, $title, "Version 1.01" & @CRLF & "June 12, 2009")
        Case $msg = $mnuQuit
            exit(0)
        Case $msg = $TRAY_EVENT_SECONDARYDOUBLE
            if $mnuQuit <> "X" Then
                TrayItemDelete($mnuQuit)
                TrayItemDelete($mnuVersion)
                TrayItemDelete($mnuDebugSep)
                $mnuQuit = "X"
                $mnuVersion = "X"
                $mnuDebugSep = "X"
                TraySetState(4)
                Sleep(2000)
                TraySetState(8)
            else
                HotKeySet("^{F12}", "TrayDebug")
                Sleep(5000)
                HotKeySet("^{F12}")
            EndIf
    EndSelect
WEnd

Func ValidIP($addr)
    if $addr <> "127.0.0.1" and $addr <> "0.0.0.0" Then
        return True
    Else
        return False
    EndIf
EndFunc

Func TrayDebug()
    if $mnuQuit <> "X" then return false
    $mnuDebugSep = TrayCreateItem("")
    $mnuVersion = TrayCreateItem("Application Version")
    $mnuQuit = TrayCreateItem("Exit Application")
    TraySetState(4)
    Sleep(2000)
    TraySetState(8)
    return true
EndFunc
Link to comment
Share on other sites

I don't see anything wrong with your code. Here's a test to see if it is a problem with windows tray.

1. Go to Control Panel

2. Click on "Taskbar and Start Menu"

3. Click "Customize" in the taskbar tab.

4. Search for your icon in the list.

5. Change the state of that icon to "Always Show"

6. Then click ok in the windows that were opened up.

If the icon still disappears, the problem has something to do with the internals of your computer. Or maybe a glitch in the script.

Edited by dantay9
Link to comment
Share on other sites

I've seen this with other applications - if Explorer crashes and restarts, some icons normally in the systray don't populate even though the task exists. Restarting the task brings the icon back as you describe.

You can test by killing Explorer in task manager and allowing it to respawn (or respawning it yourself).

Always carry a towel.

Link to comment
Share on other sites

if explorer crashes, a WM_TASKBARCREATED message is broadcast to all windows

once explorer is re-created so applications can re-create their tray icons.

apps call RegisterWindowMessage API and register message "TaskbarCreated"

AutoIt handles this message internally and re-creates the icon in the Notification Area.

some apps don't bother to monitor for this message.

this is how icons fail to re-appear in the Notification Area.

you would have to enumerate the tray icons to see if your app is not among them (hidden) and

try running TraySetState(1) or TraySetIcon(@AutoItExe, 1)

I don't see how the icon is not being re-created internally by AutoIt if that is in fact the problem

the icon is always re-created by AutoIt using the Shell_NotifyIconW API if Explorer crashes/closes.

what is AutoIt version of compiled app and machines OS? (sometimes people who post here are using older AutoIt versions and don't state it)

OS of the machines problem occurs on?

problem with icon in compiled app? (clarification: if you are adding your own icon not shown in your posted script)

Edit: example script shows how you could monitor for Explorer closure/crash and write to log

if this is an old AutoIt version bug or OS problem related to Explorer then if icon has disappeared and crash is logged that might help in troubleshooting

I can also post a tray icon enumerating script that lists visible and hidden icons

(some hidden icons are not listed in taskbar hide inactive icons list. e.g. 4 Explorer Connections Tray icons)

;example script shows how WM_TASKBARCREATED message is received
;Author: rover
;if explorer closed or crashed, WM_TASKBARCREATED message received by window after Explorer is re-created
   #AutoIt3Wrapper_Au3Check_Parameters=-d -w 1 -w 2 -w 3 -w 4 -w 5 -w 6
   #include <guiconstantsex.au3>
   #include <windowsconstants.au3>
   
   Opt('MustDeclareVars', 1)
   
   Global $WM_TASKBARCREATED
   Global $aRet = DllCall("User32.dll", "int", "RegisterWindowMessageW", "wstr", "TaskbarCreated")
   If @error Or UBound($aRet) <> 2 Then Exit
   $WM_TASKBARCREATED = $aRet[0]
   ConsoleWrite('+$WM_TASKBARCREATED = ' & Hex($WM_TASKBARCREATED) & @CRLF)
   
   _Main()
   
   Func _Main()
       GUICreate("WM_TASKBARCREATED", 400, 200)
       GUIRegisterMsg($WM_TASKBARCREATED, "WM_TASKBARCREATED")
       GUISetState()
   
       Do
       Until GUIGetMsg() = $GUI_EVENT_CLOSE
       GUIDelete()
   EndFunc;==>_Main
   
   Func WM_TASKBARCREATED($hWnd, $iMsg, $iwParam, $ilParam)
       #forceref $hWnd, $iMsg, $iwParam, $ilParam
       ConsoleWrite('!WM_TASKBARCREATED = ' & $iMsg & @CRLF)
       Beep(1000, 5)
       Return $GUI_RUNDEFMSG
   EndFunc;==>WM_TASKBARCREATED
Edited by rover

I see fascists...

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...