Jump to content

Persistent hotkey bindings, Sleep, and CPU usage


Recommended Posts

Hi all,

New AutoIt user here. With the help of the FAQ, the Wiki, and a few great forum threads, I've gotten the guts of my function to do what I want it to do, which is press a key until a pixel changes to a certain color, with a random delay between key presses. Here is an example below:

Func Check1 ()
 Do
 Send ( "{ENTER}" )
 $delay = Random(0,10000)
 Sleep($delay)
 Until PixelGetColor(1,1) = 1758338
Endfunc

Now let's say I have 3 of these Check functions, each pointing to different pixels and different colors, and I want to map them all to hotkeys that I can use while the script runs persistently until I hit a termination hotkey. Here is what I was planning to add, in addition to the Check functions:

HotKeySet("{ESC}", "Terminate")
HotKeySet("v", "Check1")
HotKeySet("b", "Check2")
HotKeySet("n", "Check3")

;Check function definitions

Func Terminate()
    Exit
EndFunc

While 1
   Sleep(3000)
Wend

What I expect this to do: map the V, B, and N keys to run through Check1, Check2, and Check3 respectively, on demand, until I hit Escape which will stop the script.

Here are my questions:

  • I've read that having a Sleep in the While 1... loop can help to prevent the script from hogging CPU resources. Will having a Sleep there interfere with my ability to use the hotkeys (V, B, N) on demand, or do the hotkey binds remain -- and allow processing the Check functions on demand as well -- even with that Sleep command?
  • Is there a different value recommended? I set it to 3 seconds of sleep. My hardware is solid, but I still don't want this script to hog resources.
  • Does the answer about Sleep interference change if I add a timeout check to each of my "Check" functions? I was thinking of adding something like this into the Check functions:
Func Check1 ()
    $timelim = 5000
    $timestart = @Sec
    do 
    If @Sec > ($timestart + $timelim) Then
      ExitLoop
     EndIf
   ;... rest of function definition
    Until PixelGetColor(1,1) = 1758338
Endfunc

...I guess I'm hoping that the ExitLoop will only exit the Do...Until loop, but not the overall While 1 loop.
 

Thank you for any advice you can offer!

Link to post
Share on other sites
1 hour ago, dw0914 said:

Will having a Sleep there interfere with my ability to use the hotkeys (V, B, N) on demand

No, hotkey will trigger the function immediately

1 hour ago, dw0914 said:

Is there a different value recommended? I set it to 3 seconds of sleep

Doesn't matter it could be 10,100,1000...  While 1...Wend uses close to 0 ressource.  So you'll be in sleep all the time. 

1 hour ago, dw0914 said:

I was thinking of adding something like this into the Check functions

No need for that, but if you want to check some form of delay use TimerInit and TimerDiff functions.

May I ask what application you want to automate ?  PixelGetColor is not the most robust approach to use...

Edited by Nine
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
  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...