Sign in to follow this  
Followers 0
corz

Tray menu ignored

6 posts in this topic

#1 ·  Posted (edited)

I just know I'm missing something obvious here, but for the life of me, I can't get this to work. In this example, the DoPause() function is completely ignored until all the commands are run, which defeats the purpose of a pause! In the real (larger) version, the pause/resume functions perfectly if I DoRun() from a GUI button/HotKey, but activating from the tray causes it to be ignored until the run is complete.

This smaller example demonstrates the problem perfectly, without all that extra code getting in the way..

#include <Constants.au3>

AutoItSetOption("TrayMenuMode", 9)
AutoItSetOption("TrayOnEventMode", 1)

global $commands[3] = [2, "compact.exe /C /S I:\work\documents\reports\*.log", "compact.exe /U /S I:\work\documents\reports\*.log"]
global $trayrun, $traypause, $trayresume, $trayexit

MakeTray()

while 1
    Sleep(10)
wend

exit


func DoRun()

    global $pause = 0

    for $i = 1 to $commands[0]
        while $pause = 1
            Sleep(250)
        wend
        $return_code = RunWait($commands[$i])
    next
    MsgBox(0, "all done", "all done")
endfunc

func MakeTray()
    $trayrun = TrayCreateItem("Run")
    TrayItemSetOnEvent(-1,"DoRun")
    TrayCreateItem("")
    $traypause = TrayCreateItem("Pause")
    TrayItemSetOnEvent(-1,"DoPause")
    $trayresume = TrayCreateItem("Resume")
    TrayItemSetOnEvent(-1,"DoResume")
    TrayItemSetState(-1, $TRAY_DISABLE)
    TrayCreateItem("")
    $trayexit = TrayCreateItem("Exit")
    TrayItemSetOnEvent(-1,"ExitScript")
    TraySetClick(8)
    TraySetState()
endfunc

func DoPause()
ConsoleWrite(".\" & @ScriptName & " (" & @ScriptLineNumber & ") : ==> " & "PAUSING: " & @LF)
    $pause = 1
    TrayItemSetState($traypause, $TRAY_DISABLE)
    TrayItemSetState($trayresume, $TRAY_ENABLE)
endfunc

func DoResume()
    $pause = 1
    TrayItemSetState($traypause, $TRAY_ENABLE)
    TrayItemSetState($trayresume, $TRAY_DISABLE)
endfunc

func ExitScript()
    exit
endfunc

So, what am I missing?

;o)

(or

[edit: oops, wee typo]

Edited by corz

nothing is foolproof to the sufficiently talented fool..

Share this post


Link to post
Share on other sites



NOT TESTED...

#include <Constants.au3>

AutoItSetOption("TrayMenuMode", 9)
AutoItSetOption("TrayOnEventMode", 1)

Global $commands[3] = [2, "compact.exe /C /S I:\work\documents\reports\*.log", "compact.exe /U /S I:\work\documents\reports\*.log"]
Global $trayrun, $traypause, $trayresume, $trayexit
Global $pause = 0, $Runner

MakeTray()

While 1
    
    If $Runner Then
        For $i = 1 To $commands[0]
            While $pause = 1
                Sleep(250)
            WEnd
            $return_code = RunWait($commands[$i])
        Next
        MsgBox(0, "all done", "all done")
    EndIf
    Sleep(10)
WEnd

Exit


Func DoRun()
    $Runner = 1
EndFunc   ;==>DoRun

Func MakeTray()
    $trayrun = TrayCreateItem("Run")
    TrayItemSetOnEvent(-1, "DoRun")
    TrayCreateItem("")
    $traypause = TrayCreateItem("Pause")
    TrayItemSetOnEvent(-1, "DoPause")
    $trayresume = TrayCreateItem("Resume")
    TrayItemSetOnEvent(-1, "DoResume")
    TrayItemSetState(-1, $TRAY_DISABLE)
    TrayCreateItem("")
    $trayexit = TrayCreateItem("Exit")
    TrayItemSetOnEvent(-1, "ExitScript")
    TraySetClick(8)
    TraySetState()
EndFunc   ;==>MakeTray

Func DoPause()
    ConsoleWrite(".\" & @ScriptName & " (" & @ScriptLineNumber & ") : ==> " & "PAUSING: " & @LF)
    $pause = 1
    TrayItemSetState($traypause, $TRAY_DISABLE)
    TrayItemSetState($trayresume, $TRAY_ENABLE)
EndFunc   ;==>DoPause

Func DoResume()
    $pause = 0
    TrayItemSetState($traypause, $TRAY_ENABLE)
    TrayItemSetState($trayresume, $TRAY_DISABLE)
EndFunc   ;==>DoResume

Func ExitScript()
    Exit
EndFunc   ;==>ExitScript

8)


NEWHeader1.png

Share this post


Link to post
Share on other sites

Understand, this is a just proof of concept of what looks (to me) like a bug - but I thought I'd better throw it here first, for more experienced eyes to see. It's code snipped-ish from a larger application where DoRun() is already a function among many others - I'd prefer not to just throw it into the idle loop! Thanks for your (super-quick!) work-around, but as far as I can tell, my code *should* work, and I'd really like to know why it doesn't!

In the real application, the DoRun() function can be called from a HotKey, a GUI button, and a tray menu item. When the exact same function is called from either HotKey or GUI button, the pause/unpause (I also have abort) functionality works perfectly, every time.

When DoRun() is called via the system tray menu item, the DoPause() function no longer works, and isn't called until the end of the loop, as if it's been buffered and stored away somewhere. It's downright weird.

Anyway, I hope that makes things a bit clearer.

;o)

(or


nothing is foolproof to the sufficiently talented fool..

Share this post


Link to post
Share on other sites

AFAIK ... its always been like that

8)


NEWHeader1.png

Share this post


Link to post
Share on other sites

So, it's a bug, and it's always been like that. That makes no sense!

Och well, I'll put together a better example and post it in the bug reports.

Thanks.

;o)

(or


nothing is foolproof to the sufficiently talented fool..

Share this post


Link to post
Share on other sites

It turns out that, for some reason, AutoIt can only handle one tray menu (or GUI) event at a time, and because the DoRun function doesn't immediately "return", AutoIt gets stuck, waiting. Very strange behaviour, but once you realize what's going on; not so difficult to work around..

func MenuDoRun()
    AdlibEnable ("DoRun")
endfunc

.. with an AdlibDisable() immediately inside the DoRun() function.

;o)

(or


nothing is foolproof to the sufficiently talented fool..

Share this post


Link to post
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
Sign in to follow this  
Followers 0