Sign in to follow this  
Followers 0
coolbk

Script and CPU usage

7 posts in this topic

Hello,

Got an easy script: when I press F1 and F2 it just does INSTANT mouse1 click in the coordinates and make mouse back in start position.

The problem: It uses from 50 to 100% cpu usage constant. Only stops when I switch it off.

Where's the problem? Also maybe there's chance to make this code easier and to make keybindings working only for certain application?

Biggest thanks!

Opt("WinDetectHiddenText",1)
Opt("MouseCoordMode",0)
Global $Paused
HotKeySet("{F3}", "Stop")

Func Stop()
Exit
EndFunc   

while 1
If WinActive("[CLASS:SunAwtFrame]") Then
HotKeySet("{F1}","turn")
HotKeySet("{F2}","pass")
#HotKeySet("{b}","turn")
#HotKeySet("{n}","pass")
Else
EndIf
WEnd

func turn()
If WinActive("[CLASS:SunAwtFrame]") Then
$pos = MouseGetPos()
MouseClick("left",590,280,1,0)
MouseMove($pos[0], $pos[1], 0)
EndIf
EndFunc

func pass()
If WinActive("[CLASS:SunAwtFrame]") Then
$pos2 = MouseGetPos()
MouseClick("left",638,279,1,0)
MouseMove($pos2[0], $pos2[1], 0)
EndIf
EndFunc

Share this post


Link to post
Share on other sites



it uses a lot of CPU because

1. The While loop dont have any sleep on it.

2. You are setting the hotkeys again and again and again and forever, because you put it in a loop, That`s tottaly wrong you should set the hotkeys at the start of the script and never re setting the hoykey if you dont have a reason to do it.

Try removing the HotKeySet() Functions from the main loop and add a little sleep inside( a sleep(do your own tests) will be enough).

Share this post


Link to post
Share on other sites

Thanks, this one solved, and also question:

How can I make to F3 pause the script fully? and then resume by pressing same hotkey

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

You will need to create a pause() function and a hotkey for that. something like this.

HotKeySet("{F3}", "_Pause")
Global $Pause = False ; This is necesary
Global $i = 0 ; This is only for the example
While True
    $i += 1
    ConsoleWrite($i & @CRLF) ; ...or any function you have in the loop
    Sleep(500)
WEnd

Func _Pause()
    $Pause = Not $Pause ; If $Pasue = FALSE then TRUE, if $Pause = TRUE then FALSE
    While $Pause
        Sleep(800)
    WEnd
EndFunc   ;==>_Pause

EDIT: About #HotKeySet("{b}","turn") ?

I don`t have any idea of what is this... is not any directive so you should remove it.

Edited by monoscout999

Share this post


Link to post
Share on other sites

You will need to create a pause() function and a hotkey for that. something like this.

HotKeySet("{F3}", "_Pause")
Global $Pause = False ; This is necesary
Global $i = 0 ; This is only for the example
While True
    $i += 1
    ConsoleWrite($i & @CRLF) ; ...or any function you have in the loop
    Sleep(500)
WEnd

Func _Pause()
    $Pause = Not $Pause ; If $Pasue = FALSE then TRUE, if $Pause = TRUE then FALSE
    While $Pause
        Sleep(800)
    WEnd
EndFunc   ;==>_Pause

EDIT: About #HotKeySet("{b}","turn") ?

I don`t have any idea of what is this... is not any directive so you should remove it.

Hmm it doesn't work. Inserted it, but it doesn't even pause the script. I need to pause it totally to make hotkeys work as default.

Share this post


Link to post
Share on other sites

My example works for me... :S

Did you see the console stop writing?

Share this post


Link to post
Share on other sites

Hmm it doesn't work. Inserted it, but it doesn't even pause the script. I need to pause it totally to make hotkeys work as default.

You don't want to pause it, you want to unset HotKeySet(). Just open helpfile and go to HotKeySet() it tells you exactly what to do.

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