Jump to content

TrayMenu problem


DickG
 Share

Recommended Posts

I am trying to run a loop that cycles every 5 seconds to do something.

I also have a Tray menu with two items: Settings and Exit.

The Tray menu appears, but does not react to any clicks while the loop is running.

I am at a loss as to what I'm doing wrong. I would really appreciate some guidance.

Here is a reproducer:

#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_Run_Before=taskkill.exe /F /IM "%scriptfile%.exe"
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****
#Region: Include
    #requireadmin
    #Include-once
    #Include <Misc.au3>
    #include <TrayConstants.au3>

#Endregion
#Region: Run
    Initialize()
    Loop()
#EndRegion
#Region: Initialize
    Func Initialize()
        Local $ThisFunc = "Initialize"

        $Title = "Tray Test"
        $Version = "0.1"

        If @OSArch = "x64" Then
            MsgBox(0, "Uh Oh", "Sorry, but this cannot run on the 64-bit version of Windows.")
            Exit
        EndIf

        ;Prevent another instance.
        If _Singleton($Title, 1) = 0 Then ProcessClose($Title & ".exe")

        ;Set options. See Function Reference > Misc. Management > AutoIt Set Option in Help file for list
        Opt("GUICloseOnESC");1=ESC closes, 0=ESC will not close
        Opt("WinTitleMatchMode", 2);1=start, 2=subStr, 3=exact, 4=advanced, -1 to -4=Nocase
        Opt("TrayMenuMode", 3);remove Exit and Script Paused tray menu items

        Return
    EndFunc
#EndRegion
#Region: Loop
    Func Loop()
        Local $ThisFunc = "Loop"

        Local $i = ""
        Local $Exit_app, $Settings

        ;Create menu items.
        $Settings = TrayCreateItem("Set period", -1, -1, 0)
        $Exit_app = TrayCreateItem("Exit")

        ;Set Tray tip.
        TraySetToolTip ("Event Log");sets tooltip
        TraySetState(1);show Tray icon
        TrayItemSetState(-1, $TRAY_ENABLE)

        MsgBox(0, $ThisFunc, "Starting loop.")

        While 1
            $i += 1

            If $i >= 5 Then ExitLoop

            MsgBox(0, $ThisFunc, "Count = " & $i)

            Switch TrayGetMsg()
                Case $Exit_app
                    MsgBox(0, $ThisFunc, "Clicked Exit.")
                    Exit

                Case $Settings
                    MsgBox(0, $ThisFunc, "Clicked Settings.")
            EndSwitch

            ;Repeat every 5 seconds.
            Sleep(5000)
        WEnd

        MsgBox(0, $ThisFunc, "Ended loop.")

        Exit

        Return
    EndFunc
#EndRegion

 

Link to comment
Share on other sites

Your Sleep function is causing you to not get the tray message when it's sent to your script. You'll have to use TrayOnEventMode or

  1. Call a function that does your sleep.
  2. Initialize a timer.
  3. While loop using your timer and the timeout you want.
  4. In the loop use your Switch (TrayGetMsg()).

Here's for the TraySetOnEventMode

#Region: Initialize
    Func Initialize()
        Local $ThisFunc = "Initialize"

        $Title = "Tray Test"
        $Version = "0.1"

        ;Prevent another instance.
        If _Singleton($Title, 1) = 0 Then ProcessClose($Title & ".exe")

        ;Set options. See Function Reference > Misc. Management > AutoIt Set Option in Help file for list
        Opt("GUICloseOnESC");1=ESC closes, 0=ESC will not close
        Opt("WinTitleMatchMode", 2);1=start, 2=subStr, 3=exact, 4=advanced, -1 to -4=Nocase
        Opt("TrayMenuMode", 3);remove Exit and Script Paused tray menu items
        Opt("TrayOnEventMode", 1)

        Return
    EndFunc
#EndRegion
#Region: Loop
    Func Loop()
        Local $ThisFunc = "Loop"

        Local $i = ""
        Local $Exit_app, $Settings

        ;Create menu items.
        $Settings = TrayCreateItem("Set period", -1, -1, 0)
        TrayItemSetOnEvent($Settings, "Settings")
        $Exit_app = TrayCreateItem("Exit")
        TrayItemSetOnEvent($Exit_app, "Close")

        ;Set Tray tip.
        TraySetToolTip ("Event Log");sets tooltip
        TraySetState(1);show Tray icon
        TrayItemSetState(-1, $TRAY_ENABLE)

        MsgBox(0, $ThisFunc, "Starting loop.")

        While 1
            $i += 1

            If $i >= 5 Then ExitLoop

            MsgBox(0, $ThisFunc, "Count = " & $i)

            ;Repeat every 5 seconds.
            Sleep(5000)
        WEnd

        MsgBox(0, $ThisFunc, "Ended loop.")

        Exit

        Return
    EndFunc
#EndRegion

Func Settings()
    MsgBox("", "", "Set Period tray item clicked")
EndFunc

Func Close()
    Exit 0
EndFunc

 

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...