Jump to content

Issues with pausing script with custom tray menu


Recommended Posts


this is just a draft. When I click the tray icon, I get the menu immediately, but once I click "script paused", everything freezes: The icon chances to the red "X", but doesn't flash constantly, just alters from "X" to "Autoit" every 20 seconds, and the next click on the tray icon can take over two minutes.

What do I miss?


Note: When I set TrayAutoPause=1, then click the tray icon, the tray icon is flashing "X" <-->"Autoit" as usual fast, as long as the tray menu is open. Once I click anything in the menu, the "flashing" is freezing as well.


#include <MsgBoxConstants.au3>
#include <StringConstants.au3>
#include <TrayConstants.au3> ; Required for the $TRAY_ICONSTATE_SHOW constant.

Opt("TrayMenuMode", 2) ; don not check / uncheck automatically


Func Example()
    Local $idTailLogMain= TrayCreateItem("Start tail main log")
    Local $idTailErrLog= TrayCreateItem("Start tail error log")
    TrayCreateItem("") ; Create a separator line.
    Local $idAbout = TrayCreateItem("About")
    TraySetState($TRAY_ICONSTATE_SHOW) ; Show the tray menu.

    While 1
        Switch TrayGetMsg()
            Case $idTailLogMain
                ; do this
            case $idTailErrLog
                ; do that
            case $idAbout
                MsgBox(64,@ScriptName,"Version: " & FileGetVersion(@ScriptFullPath) & @CRLF & _
                "Autoit Version: " & @AutoItVersion,5)
EndFunc   ;==>Example


Earth is flat, pigs can fly, and Nuclear Power is SAFE!

Link to comment
Share on other sites

This is a question on how to code it correctly, so that the tray icon will react normally, when the script is going to pause.


When using the default tray icon, then click on it with trayautopause=1, then it behaves as expected.

That's why I assume, that I made a mistake when coding my custom tray menu?

Earth is flat, pigs can fly, and Nuclear Power is SAFE!

Link to comment
Share on other sites

On 10/5/2023 at 1:28 PM, rudi said:

but once I click "script paused", everything freezes

this seems to me It's normal. If script  pauses everything freezes  :)

Unless you put  your own pause where you can managed,  while the rest of the program runs

In addition, I suggest you to put it in on event mode so that it does not get confused with the flow of the rest of the program

Here is an example

#AutoIt3Wrapper_Au3Check_Parameters=-d -w 1 -w 2 -w 3 -w 4 -w 5 -w 6 -w 7

#include <WindowsConstants.au3>
#include <TrayConstants.au3>
#include <WinAPISys.au3>
#include <Misc.au3>

#Region ===( Tray_Menu )===
Opt("TrayMenuMode", 1 + 2) ;0=append, 1=no default menu, 2=no automatic check, 4=menuitemID  not return
Opt("TrayOnEventMode", 1) ; Enable TrayOnEventMode.

Global $idExit, $idPaused, $idHelpInfo, $bPaused = True

$idHelpInfo = TrayCreateItem("Help info")
TrayItemSetOnEvent(-1, "TRAY_ShowHelp")

$idPaused = TrayCreateItem("Pause", -1, -1, 1)
TrayItemSetOnEvent(-1, "TRAY_TogglePause")
TrayItemSetState(-1, $TRAY_UNCHECKED)

TrayCreateItem("") ; ---- Create a separator line.

$idExit = TrayCreateItem("Exit")
TrayItemSetOnEvent(-1, "TRAY_Exit")

TraySetClick(8) ; (8) = Pressing secondary mouse button will display the tray menu.
TraySetOnEvent($TRAY_EVENT_PRIMARYUP, "TRAY_TogglePause")

TraySetIcon("imageres.dll", -119)
TraySetState($TRAY_ICONSTATE_SHOW)  ; Show the tray menu
TraySetToolTip("HotKeySet" & @CRLF & "Click to Pause / UnPause")
#EndRegion ===( Tray_Menu )===

;Active Window
Global $g_ActiveWnd[4] ; 0=winHandle, 1=WinTitle, 2=WinStyle, 3=WinExStyle

; Enable hotkeys
;~ HotKeySet("#^t", "_WinTogleOnTop")     ; Win+Ctrl+T
;~ HotKeySet("#^g", "_GoogleSearch")      ; Win+Ctrl+G

While Sleep(50)
    If $bPaused Then ContinueLoop

Func TRAY_Exit()
EndFunc   ;==>TRAY_Exit
Func TRAY_TogglePause()
    If $bPaused = False Then
        $bPaused = True
        TrayItemSetState($idPaused, $TRAY_CHECKED) ; $TRAY_UNCHECKED, $TRAY_CHECKED
        TraySetIcon("imageres.dll", -3)
        ; Disable hotkeys
        ConsoleWrite("Disable hotkeys" & @CRLF)
        HotKeySet("#^t")  ; Disable Win+Ctrl+T
        HotKeySet("#^g")  ; Disable Win+Ctrl+G
        $bPaused = False
        TrayItemSetState($idPaused, $TRAY_UNCHECKED) ; $TRAY_UNCHECKED, $TRAY_CHECKED
        TraySetIcon("imageres.dll", -119)
        ; Enable hotkeys
        ConsoleWrite("Enable hotkeys" & @CRLF)
        HotKeySet("#^t", "_WinTogleOnTop")     ; Win+Ctrl+T
        HotKeySet("#^g", "_GoogleSearch")      ; Win+Ctrl+G
EndFunc   ;==>TRAY_TogglePause
Func TRAY_ShowHelp()

    Local $sTxt = ""
    $sTxt &= "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" & @CRLF
    $sTxt &= "Window Togle On Top           Win+Ctrl+T" & @CRLF
    $sTxt &= "Google secected word          Win+Ctrl+G" & @CRLF
    $sTxt &= "" & @CRLF
    $sTxt &= "      click to exit" & @CRLF
    $sTxt &= " "

    ToolTip($sTxt, (@DesktopWidth * 0.75), @DesktopHeight * 0.25, " Help:", 1)

    While 1
        If _IsPressed("01") Then ExitLoop  ; 01 Left mouse button


EndFunc   ;==>TRAY_ShowHelp
Func _GetActiveWindow()
    Local $AnyWindow

    $AnyWindow = WinGetHandle("[ACTIVE]")

    If $g_ActiveWnd[0] <> $AnyWindow Then
        $g_ActiveWnd[0] = $AnyWindow
        $g_ActiveWnd[1] = WinGetTitle($g_ActiveWnd[0])
        ConsoleWrite("$g_ActiveWnd[1]=" & $g_ActiveWnd[1] & @CRLF)

        $g_ActiveWnd[2] = _WinAPI_GetWindowLong($g_ActiveWnd[0], $GWL_STYLE)
        ;ConsoleWrite("$g_ActiveWnd[2]=" & $g_ActiveWnd[2] & @CRLF)

        $g_ActiveWnd[3] = _WinAPI_GetWindowLong($g_ActiveWnd[0], $GWL_EXSTYLE)
        ;ConsoleWrite("$g_ActiveWnd[3]=" & $g_ActiveWnd[3] & @CRLF)


EndFunc   ;==>_GetActiveWindow
Func _WinTogleOnTop()
    Local $iStyle = _WinAPI_GetWindowLong($g_ActiveWnd[0], $GWL_EXSTYLE)
    Local $iStyleTopMost = BitOR($iStyle, $WS_EX_TOPMOST)
    If $iStyle = $iStyleTopMost Then
        WinSetOnTop($g_ActiveWnd[0], "", 0)
        WinSetOnTop($g_ActiveWnd[0], "", 1)
EndFunc   ;==>_WinTogleOnTop
Func _SendEx($ss, $warn = "")
    ;Send the string $ss after the Shift Alt and Ctrl keys are released.
    ;Optionally give a warning after 1 sec if any of those keys are still down.
    ;Requires misc.au3 to be included in the script for the _IsPressed function.

    Local $iT = TimerInit()

    While _IsPressed("10") Or _IsPressed("11") Or _IsPressed("12")
        If $warn <> "" And TimerDiff($iT) > 1000 Then
            MsgBox($MB_TOPMOST, "Warning", $warn)
EndFunc   ;==>_SendEx
Func _GoogleSearch()
    ;ConsoleWrite("- GoogleSearch" & @CRLF)

    Local $ClipBack = ClipGet() ; backup clip data
    ClipPut("<Empty>") ; <Empty>
    Local $sWord = ClipGet()

    If $sWord = "<Empty>" Then
        ConsoleWrite("Clipboard: " & $sWord & @CRLF)
        ShellExecute("https://www.google.gr/search?q=" & $sWord & "")

    ClipPut($ClipBack) ; Restore clip data
EndFunc   ;==>_GoogleSearch


Edited by ioa747

I know that I know nothing

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

  • Create New...