Jump to content

help loop parameter


 Share

Recommended Posts

how do I break the loop if my program is stuck in it without exiting the whole program?

 

i just want it to start from the beginning of the code here is my program

 

While 1

$picture = "target.png"

$result = _ImageSearch($picture,1,$x1,$y1,0,0)

If $result = 1 Then

   Send("{4}")
   MouseClick("left",$x1,$y1,1,1)
   Sleep(2000)


   Do
   $picture2 = "status.png"
   $result2 = _ImageSearch($picture2,1,$x1,$y1,0,0)
   
   Send("{2}")
   Send("{1}")

Until $result2 = 1

 

 

as you see if my program doesnt detect  or see picture2 then the loop wont stop.

 


 

Link to comment
Share on other sites

I would do so by creating a global variable which is evaluated to see if the loop should continue and use a hotkey to set the global variables value.  Something like this.

Global $bEsc = False    ;Define Global "state" Variable

HotKeySet("{esc}", "_Esc")  ;Set HotKey to Run _Esc function when Esc key is pressed

;....

Do
    If $bEsc = True Then    ;At beginning of loop check $bEsc value and If True act on it
        $bEsc = False   ;Reset $bEsc value
        ExitLoop        ;Exit Loop
    EndIf
Until ;...you get the idea?

Func _Esc()     ;Function to set $bEsc value to True
    $bEsc = True
EndFunc

 

Link to comment
Share on other sites

3 hours ago, spudw2k said:

I would do so by creating a global variable which is evaluated to see if the loop should continue and use a hotkey to set the global variables value.  Something like this.

Global $bEsc = False    ;Define Global "state" Variable

HotKeySet("{esc}", "_Esc")  ;Set HotKey to Run _Esc function when Esc key is pressed

;....

Do
    If $bEsc = True Then    ;At beginning of loop check $bEsc value and If True act on it
        $bEsc = False   ;Reset $bEsc value
        ExitLoop        ;Exit Loop
    EndIf
Until ;...you get the idea?

Func _Esc()     ;Function to set $bEsc value to True
    $bEsc = True
EndFunc

 

Hi is there a way that my program can automatically checked if its stuck in the loop?

Link to comment
Share on other sites

You'd have to define some sort of condition of what stuck means: been looping for so long? looped x many times?

I'd say a timer is a reasonable way to go, but like I said, it's up to you to define a threshold that would mean stuck to you.

;More pseudo-code

;Create timer outside of loop
Local $hTimer = TimerInit()

;Inside of Loop
Do
    If TimerDiff($hTimer) > 30000 Then   ;<- Timer measured in milliseconds - 30 seconds X 1000 milliseconds = 30,000 milliseconds
        $hTimer = TimerInit()   ;Reset Timer
        ;do stuff
    EndIf
Until 0=1 ;something

 

Link to comment
Share on other sites

5 hours ago, spudw2k said:

You'd have to define some sort of condition of what stuck means: been looping for so long? looped x many times?

I'd say a timer is a reasonable way to go, but like I said, it's up to you to define a threshold that would mean stuck to you.

;More pseudo-code

;Create timer outside of loop
Local $hTimer = TimerInit()

;Inside of Loop
Do
    If TimerDiff($hTimer) > 30000 Then   ;<- Timer measured in milliseconds - 30 seconds X 1000 milliseconds = 30,000 milliseconds
        $hTimer = TimerInit()   ;Reset Timer
        ;do stuff
    EndIf
Until 0=1 ;something

 

I think this timer would do the trick thank you. :)

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

×
×
  • Create New...