Jump to content

Tray Event not working from the second try.


Anoop
 Share

Recommended Posts

Hi All,

I am facing an issue with tray event. The tray event is not getting captured from the second time. It correctly worked for the very first time.

I have copied my code below.  

There is a simple gui with a "START" button. When the button is clicked, it just sends a winhttp request continuously with 5 seconds delay.

A tray menu item "Stop" is there and on selecting it, it asks whether to stop the operation - It works perfectly for the first time. But after I confirm to stop and again START the operation from GUI. Now try to select "Stop" tray menu option. It does not work. Can someone be kindful to help?

 

#include <MsgBoxConstants.au3>
#include <GUIConstantsEx.au3>


Local $oMyError1 = ObjEvent("AutoIt.Error", "WinhttpError", "IWinHttpRequestEvents")
Local $oHTTP = ObjCreate("winhttp.winhttprequest.5.1")

Opt("TrayAutoPause",0)
Opt("TrayMenuMode",1 + 2)
Opt("TrayOnEventMode",1)

Local $oStopTray = TrayCreateItem("Stop")
TrayItemSetOnEvent(-1,"_stop")

$hGUI = GUICreate("Tray Event",100,50)
$hButton = GUICtrlCreateButton("START", 10, 10, 75, 25)

GUI()

Func GUI()

    Opt("TrayIconHide",1)
    GUISetState(@SW_SHOW)

    While 1
        $nMsg = GUIGetMsg()
        Switch $nMsg
           Case $GUI_EVENT_CLOSE
                      Exit
            Case $hButton
                GUISetState(@SW_HIDE)
                Start()
        EndSwitch
    WEnd

EndFunc

Func Start()

    Opt("TrayIconHide",0)

    while 1
        $oHTTP.Open("GET", "http://www.google.com")
        $oHTTP.Send()
        ConsoleWrite($oHTTP.ResponseBody)
        Sleep(5000)
    WEnd

EndFunc

Func _stop()

    $iReply = MsgBox($MB_YESNO + $MB_ICONINFORMATION + $MB_TOPMOST,"STOP???", "Analysis in progress. Do you want to stop the operation?")

    If $iReply == $IDYES Then
        GUI()
    Else
        Return
    EndIf

EndFunc

Func WinhttpError()

    ConsoleWrite(@ScriptName & " (" & $oMyError1.scriptline & ") : ==> COM Error intercepted !" & @CRLF & _
            @TAB & "err.number is: " & @TAB & @TAB & "0x" & Hex($oMyError1.number) & @CRLF & _
            @TAB & "err.windescription:" & @TAB & $oMyError1.windescription & @CRLF & _
            @TAB & "err.description is: " & @TAB & $oMyError1.description & @CRLF & _
            @TAB & "err.source is: " & @TAB & @TAB & $oMyError1.source & @CRLF & _
            @TAB & "err.helpfile is: " & @TAB & $oMyError1.helpfile & @CRLF & _
            @TAB & "err.helpcontext is: " & @TAB & $oMyError1.helpcontext & @CRLF & _
            @TAB & "err.lastdllerror is: " & @TAB & $oMyError1.lastdllerror & @CRLF & _
            @TAB & "err.scriptline is: " & @TAB & $oMyError1.scriptline & @CRLF & _
            @TAB & "err.retcode is: " & @TAB & "0x" & Hex($oMyError1.retcode) & @CRLF & @CRLF)

EndFunc

thanks very much

Anoop

Link to comment
Share on other sites

Not sure if this is what you are looking for???

 

#include <MsgBoxConstants.au3>
#include <GUIConstantsEx.au3>


Global $oMyError1 = ObjEvent("AutoIt.Error", "WinhttpError", "IWinHttpRequestEvents")
Global $oHTTP = ObjCreate("winhttp.winhttprequest.5.1")
Global $bStop = False

Opt("TrayAutoPause", 0)
Opt("TrayMenuMode", 1 + 2)
Opt("TrayOnEventMode", 1)

Global $oStopTray = TrayCreateItem("Stop")
TrayItemSetOnEvent(-1, "_stop")

$hGUI = GUICreate("Tray Event", 100, 50)
$hButton = GUICtrlCreateButton("START", 10, 10, 75, 25)

GUI()

Func GUI()
    Local $bButtonPressed = False
    Opt("TrayIconHide", 1)
    GUISetState(@SW_SHOW, $hGUI)

    While 1
        $nMsg = GUIGetMsg()
        Switch $nMsg
            Case $GUI_EVENT_CLOSE
                GUISetState(@SW_HIDE, $hGUI)
                GUIDelete($hGUI)

                Exit
            Case $hButton
                $bButtonPressed = True
                GUISetState(@SW_HIDE, $hGUI)
                ;Start()
                ExitLoop
        EndSwitch
    WEnd
    If $bButtonPressed Then Start()
EndFunc   ;==>GUI

Func Start()

    Opt("TrayIconHide", 0)

    While 1
        $oHTTP.Open("GET", "http://www.google.com")
        $oHTTP.Send()
        ConsoleWrite($oHTTP.ResponseBody)
        Sleep(5000)
        If $bStop Then ExitLoop
    WEnd

EndFunc   ;==>Start

Func _stop()
    $bStop = True
    $iReply = MsgBox($MB_YESNO + $MB_ICONINFORMATION + $MB_TOPMOST, "STOP???", "Analysis in progress. Do you want to stop the operation?")

    If $iReply == $IDYES Then

        GUI()
    Else
        Return
    EndIf

EndFunc   ;==>_stop

Func WinhttpError()

    ConsoleWrite(@ScriptName & " (" & $oMyError1.scriptline & ") : ==> COM Error intercepted !" & @CRLF & _
            @TAB & "err.number is: " & @TAB & @TAB & "0x" & Hex($oMyError1.number) & @CRLF & _
            @TAB & "err.windescription:" & @TAB & $oMyError1.windescription & @CRLF & _
            @TAB & "err.description is: " & @TAB & $oMyError1.description & @CRLF & _
            @TAB & "err.source is: " & @TAB & @TAB & $oMyError1.source & @CRLF & _
            @TAB & "err.helpfile is: " & @TAB & $oMyError1.helpfile & @CRLF & _
            @TAB & "err.helpcontext is: " & @TAB & $oMyError1.helpcontext & @CRLF & _
            @TAB & "err.lastdllerror is: " & @TAB & $oMyError1.lastdllerror & @CRLF & _
            @TAB & "err.scriptline is: " & @TAB & $oMyError1.scriptline & @CRLF & _
            @TAB & "err.retcode is: " & @TAB & "0x" & Hex($oMyError1.retcode) & @CRLF & @CRLF)

EndFunc   ;==>WinhttpError

 

 

EDIT: the delay in showing the GUI is because of the sleep

Edited by nitekram

All by me:

"Sometimes you have to go back to where you started, to get to where you want to go." 

"Everybody catches up with everyone, eventually" 

"As you teach others, you are really teaching yourself."

From my dad

"Do not worry about yesterday, as the only thing that you can control is tomorrow."

 

WindowsError.gif

WIKI | Tabs; | Arrays; | Strings | Wiki Arrays | How to ask a Question | Forum Search | FAQ | Tutorials | Original FAQ | ONLINE HELP | UDF's Wiki | AutoIt PDF

AutoIt Snippets | Multple Guis | Interrupting a running function | Another Send

StringRegExp | StringRegExp Help | RegEXTester | REG TUTOR | Reg TUTOT 2

AutoItSetOption | Macros | AutoIt Snippets | Wrapper | Autoit  Docs

SCITE | SciteJump | BB | MyTopics | Programming | UDFs | AutoIt 123 | UDFs Form | UDF

Learning to script | Tutorials | Documentation | IE.AU3 | Games? | FreeSoftware | Path_Online | Core Language

Programming Tips

Excel Changes

ControlHover.UDF

GDI_Plus

Draw_On_Screen

GDI Basics

GDI_More_Basics

GDI Rotate

GDI Graph

GDI  CheckExistingItems

GDI Trajectory

Replace $ghGDIPDll with $__g_hGDIPDll

DLL 101?

Array via Object

GDI Swimlane

GDI Plus French 101 Site

GDI Examples UEZ

GDI Basic Clock

GDI Detection

Ternary operator

Link to comment
Share on other sites

  • Developers
23 minutes ago, Anoop said:

 

I found alternate way. But It seems, it is a bug in AutoIt software.

 

Not a bug in Autoit3 but rather a design flaw in your script as you never return from start() ! 
Rethink the approach of your script and you'll see it works fine as demonstrated by @nitekram's example.

Jos

Edited by Jos

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

Hi @nitekram

Thanks for the reply. This is the alternate way I found. But I liked your quick response.

Hi @Jos and @nitekram,

If it is the issue with return, how it worked for the first time. I am triggering the first event when in start(). It worked correctly.

Then again coming to start() and triggering the second event. Now it is not working.

Moreover, this is OnEvent mode and my understanding is that this event should be captured regardless of where the script is.

Link to comment
Share on other sites

  • Developers
29 minutes ago, Anoop said:

Moreover, this is OnEvent mode and my understanding is that this event should be captured regardless of where the script is.

Not really ...  an event only fires one time and can only fire the next time when it returned from the initial Call.
Your original script has this call sequence: GUI() -> Start() -> TrayEvent:Stop() -> GUI() -> Start()
So as you can see: the TrayEvent never returns so thinks it is still active.

This would be my preferred solution simply Hiding and restoring the same GUI each time:

#include <MsgBoxConstants.au3>
#include <GUIConstantsEx.au3>


Global $oMyError1 = ObjEvent("AutoIt.Error", "WinhttpError", "IWinHttpRequestEvents")
Global $oHTTP = ObjCreate("winhttp.winhttprequest.5.1")
Global $bStop = False

Opt("TrayAutoPause", 0)
Opt("TrayMenuMode", 1 + 2)
Opt("TrayOnEventMode", 1)

Global $oStopTray = TrayCreateItem("Stop")
TrayItemSetOnEvent(-1, "_stop")

$hGUI = GUICreate("Tray Event", 100, 50)
$hButton = GUICtrlCreateButton("START", 10, 10, 75, 25)
Global $Start_State = 0
GUI()

Func GUI()
    Opt("TrayIconHide", 1)
    GUISetState(@SW_SHOW, $hGUI)

    While 1
        $nMsg = GUIGetMsg()
        Switch $nMsg
            Case $GUI_EVENT_CLOSE
                GUISetState(@SW_HIDE, $hGUI)
                GUIDelete($hGUI)
                Exit
            Case $hButton
                Start()
        EndSwitch
    WEnd
EndFunc   ;==>GUI

Func Start()
    Opt("TrayIconHide", 0)
    GUISetState(@SW_HIDE, $hGUI)
    $Start_State = 1
    While $Start_State
;~         $oHTTP.Open("GET", "http://www.google.com")
;~         $oHTTP.Send()
;~         ConsoleWrite($oHTTP.ResponseBody)
        ConsoleWrite(" Running..")
        Sleep(5000)
    WEnd
    GUISetState(@SW_SHOW, $hGUI)
EndFunc   ;==>Start

Func _stop()
    $iReply = MsgBox($MB_YESNO + $MB_ICONINFORMATION + $MB_TOPMOST, "STOP???", "Analysis in progress. Do you want to stop the operation?")
    If $iReply == $IDYES Then
        $Start_State=0
        ConsoleWrite(" Stop selected...")
    Else
        Return
    EndIf
EndFunc   ;==>_stop

Func WinhttpError()

    ConsoleWrite(@ScriptName & " (" & $oMyError1.scriptline & ") : ==> COM Error intercepted !" & @CRLF & _
            @TAB & "err.number is: " & @TAB & @TAB & "0x" & Hex($oMyError1.number) & @CRLF & _
            @TAB & "err.windescription:" & @TAB & $oMyError1.windescription & @CRLF & _
            @TAB & "err.description is: " & @TAB & $oMyError1.description & @CRLF & _
            @TAB & "err.source is: " & @TAB & @TAB & $oMyError1.source & @CRLF & _
            @TAB & "err.helpfile is: " & @TAB & $oMyError1.helpfile & @CRLF & _
            @TAB & "err.helpcontext is: " & @TAB & $oMyError1.helpcontext & @CRLF & _
            @TAB & "err.lastdllerror is: " & @TAB & $oMyError1.lastdllerror & @CRLF & _
            @TAB & "err.scriptline is: " & @TAB & $oMyError1.scriptline & @CRLF & _
            @TAB & "err.retcode is: " & @TAB & "0x" & Hex($oMyError1.retcode) & @CRLF & @CRLF)

EndFunc   ;==>WinhttpError

Jos

Edited by Jos

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

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