Jump to content

GUIGetMSG and sleep


Ejoc
 Share

Recommended Posts

This is far far from complete, so ignore the fact that i cant change values at the moment, for I am stuck elsewhere.

When I run this script if I dont het my hotkey d, the window will close when I try closing it (YEA).

If the hot key has been hit once, i enter my state where I want it to execute code and sleep, but it's not registering $GUI_EVENT_CLOSE events.(BOO)

But it I hit my hot key again, it will register $GUI_EVENT_CLOSE(YEA)

I know it got to be something simple, like I need to flush out events or something, anyone know the answer?

Thanks

#include <GUIConstants.au3>

$bExecute   = 0
$iGUIWidth  = 300
$iGUIHeight = 200

$hGUI       = GUICreate("")
$hHotKey    = GUICtrlCreateInput("d",10,10,$iGUIWidth-20)
$hDelay     = GUICtrlCreateInput("2000",10,30)

GUISetState()



HotKeySet(GUICtrlRead($hHotKey),"ToggleFunc")  

$iDelay = GUICtrlRead($hDelay)

While 1
    $msg = GUIGetMSG()
    Select
        case $msg = $GUI_EVENT_CLOSE
            GUIDelete()
            Exit
        Case Else
            if $bExecute Then
            ;Send("hh")
                sleep($iDelay)
            EndIf
    EndSelect
Wend



Func ToggleFunc()
    $bExecute = BitXOR($bExecute,1)
EndFunc
Start -> Programs -> AutoIt v3 -> AutoIt Help File -> Index -> (The Function you are asking about)----- Links -----DllStruct UDFsRSA Crypto UDFs
Link to comment
Share on other sites

  • Developers

If the hot key has been hit once, i enter my state where I want it to execute code and sleep, but it's not registering $GUI_EVENT_CLOSE events.(BOO)

But it I hit my hot key again, it will register $GUI_EVENT_CLOSE(YEA)

I know it got to be something simple, like I need to flush out events or something, anyone know the answer?

<{POST_SNAPBACK}>

You don't want to sleep(2000) in the While 1... Wend loop. It needs to constantly loop around to get the Messages from the queue.

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

This version works like I want, but I added a level of time checking. I still dont get why the original hangs, seems like it should still be executing GuiGetMSG() after each sleep like it is in this version.

/shrug

#include <GUIConstants.au3>
#include <Date.au3>

$bExecute   = 0
$iGUIWidth  = 300
$iGUIHeight = 105

$hGUI       = GUICreate("S2 Trainer",$iGUIWidth,$iGUIHeight)
$hHotKey    = GUICtrlCreateInput("^!d",70,10,$iGUIWidth-80)
$hDelay     = GUICtrlCreateInput("2",70,30)
$hCommand   = GUICtrlCreateInput("hh",70,50)
$hButton    = GUICtrlCreateButton("Run",($iGUIWidth-60)/2,75,60)

GUICtrlCreateLabel("HotKey",10,13)
GUICtrlCreateLabel("Delay(sec)",10,33)
GUICtrlCreateLabel("Command",10,53)

GUISetState(@SW_SHOW)

While 1
    $msg    = GUIGetMSG()
    Select
        Case $msg = $GUI_EVENT_CLOSE
            Exit
        Case $msg = $hButton
            HotKeySet(GUICtrlRead($hHotKey),"ToggleFunc")  
            $iDelay     = GUICtrlRead($hDelay)
            $iExecTime  = Time() + $iDelay
            $szCommand  = GUICtrlRead($hCommand)
            GUISetState(@SW_MINIMIZE)
    EndSelect

    if $bExecute Then
        $iNow   = Time()
        if $iNow >= $iExecTime Then
            Send($szCommand)
            $iExecTime  = $iNow + $iDelay
            sleep(1000)
        Endif
    EndIf
Wend



Func ToggleFunc()
    $bExecute = BitXOR($bExecute,1)
EndFunc

Func Time()
    return _DateDiff( 's',"1970/01/01 00:00:00",_NowCalc())
EndFunc
Start -> Programs -> AutoIt v3 -> AutoIt Help File -> Index -> (The Function you are asking about)----- Links -----DllStruct UDFsRSA Crypto UDFs
Link to comment
Share on other sites

This version works like I want, but I added a level of time checking.  I still dont get why the original hangs, seems like it should still be executing GuiGetMSG() after each sleep like it is in this version.

The original would defaulting to the Case Else statement. So your script, would be sleeping most of the time.
Link to comment
Share on other sites

The original would defaulting to the Case Else statement. So your script, would be sleeping most of the time.

<{POST_SNAPBACK}>

But here's the "problem"

In the infinate loop:

First check for an event, this will usally return 0 (there isnt one)

Sleep for awhile

Go back to the start of the loop

While it is sleeping if I send it $GUI_EVEN_CLOSE, when it finsihes sleeping and goes back to the start of the loop, I should see $GUI_EVENT_CLOSE waiting for me. But I'm not, thats where I'm puzzled.

Start -> Programs -> AutoIt v3 -> AutoIt Help File -> Index -> (The Function you are asking about)----- Links -----DllStruct UDFsRSA Crypto UDFs
Link to comment
Share on other sites

Try this change. Added $bExecute = 0 ; Reset in the Case Else statement.

#include <GUIConstants.au3>

$bExecute   = 0
$iGUIWidth  = 300
$iGUIHeight = 200

$hGUI       = GUICreate("")
$hHotKey    = GUICtrlCreateInput("d",10,10,$iGUIWidth-20)
$hDelay     = GUICtrlCreateInput("2000",10,30)

GUISetState()



HotKeySet(GUICtrlRead($hHotKey),"ToggleFunc")  

$iDelay = GUICtrlRead($hDelay)

While 1
    $msg = GUIGetMSG()
    Select
        case $msg = $GUI_EVENT_CLOSE
            GUIDelete()
            Exit
        Case Else
            if $bExecute Then
            MsgBox(0, '', '')
                sleep($iDelay)
            EndIf
            $bExecute = 0; Reset
    EndSelect
Wend



Func ToggleFunc()
    $bExecute = BitXOR($bExecute,1)
EndFunc
Link to comment
Share on other sites

setting $bExecute = 0 will only let it execute once per hotkey press, which wont work since i want it to keep executing code every 2 seconds...

This is a simpler example:

#include <GUIConstants.au3>
GUICreate("test")
GuiSetState()
While 1
    $msg = GUIGetMSG()
    if $msg = $GUI_EVENT_CLOSE Then Exit
    sleep(1000)
Wend

Why does this hang, yes its because there is a sleep statement, but it shouldn't cause it to prevent GUIGetMSG() from seeing $GUI_EVENT_CLOSE but it does.

Start -> Programs -> AutoIt v3 -> AutoIt Help File -> Index -> (The Function you are asking about)----- Links -----DllStruct UDFsRSA Crypto UDFs
Link to comment
Share on other sites

setting $bExecute = 0 will only let it execute once per hotkey press, which wont work since i want it to keep executing code every 2 seconds...

This is a simpler example:

#include <GUIConstants.au3>
GUICreate("test")
GuiSetState()
While 1
    $msg = GUIGetMSG()
    if $msg = $GUI_EVENT_CLOSE Then Exit
    sleep(1000)
Wend

Why does this hang, yes its because there is a sleep statement, but it shouldn't cause it to prevent GUIGetMSG() from seeing $GUI_EVENT_CLOSE but it does.

GUIGetMSG() is not an Adlib function, so if you pause the script, with Sleep(), It receives nil.

In this example, GUIGetMSG() sleeps for 1 second per loop cycle. It will NOT receive events, for that 1 second per loop cycle. This makes it hard for you, to send a message to it, as you need to send, when it IS receiving. What are the odds of that happening, very, very slim odds.

:)

Link to comment
Share on other sites

GUIGetMSG() is not an Adlib function, so if you pause the script, with Sleep(), It receives nil.

In this example, GUIGetMSG() sleeps for 1 second per loop cycle. It will NOT receive events, for that 1 second per loop cycle. This makes it hard for you, to send a message to it, as you need to send, when it IS receiving. What are the odds of that happening, very, very slim odds.

:)

<{POST_SNAPBACK}>

Thanks! That makes sense, I assumed Windowz put events in a stack and popped em off during GUIGetMSG() calls, not real-time catch it or loose it.
Start -> Programs -> AutoIt v3 -> AutoIt Help File -> Index -> (The Function you are asking about)----- Links -----DllStruct UDFsRSA Crypto UDFs
Link to comment
Share on other sites

Confused again :)

I did more research and in C++:

while(GetMessage(&msg, NULL, 0, 0)) {
             TranslateMessage(&msg);
             DispatchMessage (&msg);
       }

The Events are Queued, like I thought GUIGetMSG() did.

Why are the events not queued in GUIGetMSG()....

Edited by Ejoc
Start -> Programs -> AutoIt v3 -> AutoIt Help File -> Index -> (The Function you are asking about)----- Links -----DllStruct UDFsRSA Crypto UDFs
Link to comment
Share on other sites

Ok I finally figured it out and have a true solution w/ a reason:

GUIGetMSG() does pull Events Queued up by windows, so you dont have to catch events in real time.

The GUI is getting flodded w/ $GUI_EVENT_MOUSEMOVE's. So when you put in a sleep() the Queue for your window is filled with Mouse moves, and it will sleep() for each of those mouse moves before getting to the $GUI_EVENT_CLOSE.

so w/ the code:

#include <GUIConstants.au3>
GUICreate("test")
GuiSetState()
While 1
    $msg = GUIGetMSG()
    if $msg = $GUI_EVENT_CLOSE Then Exit
    sleep(1000)
Wend

We sleep for 1 second.

We click close during the sleep()

Our Queue is now $GUI_EVENT_MOUSEMOVE, $GUI_EVENT_MOUSEMOVE, $GUI_EVENT_MOUSEMOVE, $GUI_EVENT_CLOSE

We have to sleep for 1 second for each $GUI_EVENT_MOUSEMOVE before closing so in this case 3 seconds before closing

Now w/ the new code:

#include <GUIConstants.au3>
GUICreate("test")
GuiSetState()
While 1
    $msg = GUIGetMSG()
    if $msg = $GUI_EVENT_CLOSE Then Exit
    if $msg = 0 Then sleep(1000)
Wend

We sleep for 1 second.

Click close during the sleep()

Our Queue is now $GUI_EVENT_MOUSEMOVE, $GUI_EVENT_MOUSEMOVE, $GUI_EVENT_MOUSEMOVE, $GUI_EVENT_CLOSE

Now we dont do anything if it was a $GUI_EVENT_MOUSEMOVE

And process the $GUI_EVENT_CLOSE nearly immediately after we are done sleeping

Edited by Ejoc
Start -> Programs -> AutoIt v3 -> AutoIt Help File -> Index -> (The Function you are asking about)----- Links -----DllStruct UDFsRSA Crypto UDFs
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...