Jump to content

How do I sleep a function, not the script.


Recommended Posts

Allo again,

I'm full of questions tonight. Heh, anyways, I've got a problem with the sleep function. Is there a way to make the sleep function a local event instead of global? For example:

HotKeySet("{SPACE}","Auto_Click")

While 1
;Do stuff here
WEnd

Func Auto_Click()
    MouseClick("left")
    Sleep(GUICtrlRead($Click_Speed))
EndFunc

The problem with the above is that if I hold down the hotkey for let's say... 3 seconds, the whole script is pausing exponentially (I suppose because the HotKey is polled every other millisecond or so). I dunno if it is possible to do, hence me coming to you code monkies.

Thanx,

-CMR

Link to comment
Share on other sites

Well, I'm partially there. I've added this:

HotKeySet("{SPACE}","Auto_Click")
Global $Start = TimerInit()
Global $End = 0

;Fix:
Opt("MouseClickDelay",0)

While 1
;Do stuff here
    Sleep(100)
WEnd

Func Auto_Click()
    $End = TimerDiff($Start)
    If ($End >= GUICtrlRead($Click_Speed)) Then
        MouseClick("left")
       ;Or to bypass setting the option:
       ;MouseDown("left")
       ;MouseUp("left)
        $Start = TimerInit()
        $End = 0;let's make sure it is reset anyway.
    EndIf
EndFunc

Now, in my mind, it should work properly. Though in action it will click once like it is supposed to, but then it will click 2 or 3 times in a row every 6th or 7th scheduled click.

Is it TimerDiff() causing the problem? It seems to be the only possibility. Also, is there a better way?

Thanx,

-CMR

Edit: Solved. The problem was the MouseClick Delay option. It still uses the sleep command, thus making the whole script queue actions until the Sleep was over. So 10 ms default was exponentially increasing, thus causing the stutter.

Edited by CodeMaster Rapture
Link to comment
Share on other sites

Possible other solution for anyone who's interested: disable the hotkey at the beginning of the Auto_Click() function and re-enable it at the end:

Func Auto_Click()
    HotkeySet("{SPACE}")
    MouseClick("left")
    Sleep(GUICtrlRead($Click_Speed))
    HotkeySet("{SPACE}","Auto_Click")
EndFunc
Link to comment
Share on other sites

just by the by...

as far as i can see/remember we cant use HotkeySet to detect things like:

mouse down/clicks window events....

so i use GUIGetMsg to detect these...

i use it instead of sleeps too if i can get away with it... in window detection stuff .. eg

waiting for a process to exist etc etc...

Link to comment
Share on other sites

Possible other solution for anyone who's interested: disable the hotkey at the beginning of the Auto_Click() function and re-enable it at the end:

Func Auto_Click()
    HotkeySet("{SPACE}")
    MouseClick("left")
    Sleep(GUICtrlRead($Click_Speed))
    HotkeySet("{SPACE}","Auto_Click")
EndFunc

<{POST_SNAPBACK}>

Well, the problem was mainly that my function would hold up the entire script. I just wanted that specific function to sleep while the rest of the script go on its merry little way. But, yes, that would work.

-CMR

Link to comment
Share on other sites

Well, the problem was mainly that my function would hold up the entire script. I just wanted that specific function to sleep while the rest of the script go on its merry little way. But, yes, that would work.

-CMR

<{POST_SNAPBACK}>

from a Hobbyist point of view

#include <GuiConstants.au3>
Dim $stop = 0, $go = 0, $time, $t, $move

GuiCreate("The  W A I T E R ", 365, 316,(@DesktopWidth-392)/2, (@DesktopHeight-316)/2 , $WS_OVERLAPPEDWINDOW + $WS_VISIBLE + $WS_CLIPSIBLINGS)

$input1 = GuiCtrlCreateInput("wait time", 130, 85, 55, 22)
GUICtrlSetState( -1, $GUI_FOCUS)
$label1 = GuiCtrlCreateLabel("Please type in a  *wait*  time for the function ( in seconds)", 20, 150, 340, 80)
$startbutton = GuiCtrlCreateButton("Start", 130, 115, 96, 23)
GUICtrlSetState( -1, $GUI_DEFBUTTON)
$stopbutton = GuiCtrlCreateButton("Stop", 249, 115, 96, 23)
$progress = GUICtrlCreateProgress( 20, 116, 96, 20)
GUISetState()

;Set on event-- does not use GUIGetMessage()
Opt("GUIOnEventMode", 1) ; Change to OnEvent mode 

GUISetOnEvent($GUI_EVENT_CLOSE, "CLOSEClicked")
GUICtrlSetOnEvent($startbutton, "startit")
GUICtrlSetOnEvent($stopbutton, "stopit")


While 1
    
    If $go = 1 Then
        
        $t = $t +1
        $move = $move + 10
        
        GUICtrlSetData($progress, $move)
        If $t = 11 then 
            $go = 0
            $t = 0
            $move = 0
            GUICtrlSetData($progress, 0)
            GUICtrlSetData($label1, " The wait is over")
        EndIf
        
        Sleep($time)
        
    EndIf

    If $stop = 1 Then
        MsgBox(0, "Cancelled", "Wait was cancelled...", 2)
        GUICtrlSetData($progress, 0)
        $stop = 0
        $go = 0
        $t = 0
        $move = 0
    EndIf
    
    If $t <> 0 Then
        GUICtrlSetData($label1, " The Program is still running,  " & $move & "%" )
    Else
        Sleep(300)
        GUICtrlSetData($label1, "Please type in a  *wait*  time for the function ( in seconds)")
    EndIf

;Sleep(5)
WEnd

Func stopit()
   $stop = 1
EndFunc

Func startit()
    $go = 1
    $inputread = GUICtrlRead($input1)
    $time = $inputread * 90
Endfunc

Func CLOSEClicked()
  MsgBox(0, "Close Program", "You clicked CLOSE!" & @CRLF & "Now Exiting...  The W A I T E R", 3)
  Exit
EndFunc

could be a lot more accurate with a timer

8)

NEWHeader1.png

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