Sign in to follow this  
Followers 0
beestinga

Constant Checking

14 posts in this topic

How do I make my program constantly monitor for a change in a given variable and then interrupt whatever it's doing once that variable changes?

I was thinking a while loop, but that only checks to see if the variable has changed at the end of each run through, right? So I wouldn't be able to stop it from completing its loop mid-way through.

Does any of this make sense?

Share this post


Link to post
Share on other sites



Your brief summary makes sense, for what it is worth. There can be more then one solution, but the best would suit the script that you have created needs.

Share this post


Link to post
Share on other sites

I agree with MHz, if you take a look at the help file under HotKeySet() you may take a close look at TogglePause() and how it can change thoughout the script yet still retain a certain value at the same time.


[center]Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.[/center]

Share this post


Link to post
Share on other sites

You are talking about a AutoIt variable?

If that is the case wrap the variable changes in a function call then you have a monitor on the variable.

This sample code is not tested! It does not work to good when running on linux :)

$privVariableToMonitor; Do not use this in your code! Only use through the variableToMonitor call
; Call your monitor 
Do
   variableToMonitor( (variableToMonitor() + 2/3) )
Until GetGuiMsg() = -3
Exit

Func variableToMonitor($var = "ONLY_RETURN_VALUE")
   If $var <> "ONLY_RETURN_VALUE" Then 
      msgbox(0, "VALUE CHANGED", $privVariableToMonitor & "==>" &  $var, 5)
      $privVariableToMonitor = $var
      If $var > 3 Then 
         Exit
      EndIf
   EndIf
   Return $privVariableToMonitor
EndFunc

Probably not what you wanted, but you might spinn on it?

Share this post


Link to post
Share on other sites

I have a set of functions that the program is doing, and it does them all fine.

The moment something seemingly unrelated to my functions occurs, I want to quit whatever function is currently going and call a specific one.

Like, if my function looked like this:

do some stuff up here
maybe create a gui here
click some buttons and stuff
maybe sleep(1500) or something here

and the whole while, if pixelchecksum() for a certain area changes to a specific value, I want it to go to:

func ohshit()
msgbox(0,"ruh-roh!","Bail man, Bail!")
exit
endfunc

If that's what your sample script does, please explain to me how.

Sorry for bringing up an old, dead thread, but I've been working on other stuff that's kept me away from my AutoIt work.

Share this post


Link to post
Share on other sites

This one uses Coroutine.au3 from my signature:

#include <Coroutine.au3>

$mainFunction = _CoCreate('Func mainFunction()| ;Not really sure what you want to do in your main loop, but this is what you had:|While 1|  ;do some stuff| ;create a gui|  ToolTip("Main script still running.", 0, 0)|    Sleep(1500)|WEnd|EndFunc')
$pid = _CoStart($mainFunction)

While 1
    If PixelGetColor(100,100) == "0x8e5a00" Then ; Some color
        _CoKill($pid)
        MsgBox(0,"ruh-roh", "Bail Man, Bail!")
        _CoCleanup()
        Exit
    EndIf
    Sleep(10)
WEnd

Of course this is just a very simple example, but hopefully you get the picture.


[u]My UDFs[/u]Coroutine Multithreading UDF LibraryStringRegExp GuideRandom EncryptorArrayToDisplayString"The Brain, expecting disaster, fails to find the obvious solution." -- neogia

Share this post


Link to post
Share on other sites

#include <Coroutine.au3>

$mainFunction = _CoCreate('Func mainFunction()| ;Not really sure what you want to do in your main loop, but this is what you had:|While 1|  ;do some stuff| ;create a gui|  ToolTip("Main script still running.", 0, 0)|    Sleep(1500)|WEnd|EndFunc')
$pid = _CoStart($mainFunction)

While 1
    If PixelGetColor(100,100) == "0x8e5a00" Then ; Some color
        _CoKill($pid)
        MsgBox(0,"ruh-roh", "Bail Man, Bail!")
        _CoCleanup()
        Exit
    EndIf
    Sleep(10)
WEnd

well, I'm not sure I entirely get the picture. I sort of get it, but I'm not sure how to implement it.

To add this feature to my existing code, would I just put all of my code into that _CoCreate( function? Currently, my code consists of many different functions, all flopping around and calling one another and all of these fancy things. It's about 300 lines right now - would that all go into the _CoCreate function?

Also, I noticed you're using while loops. If the condition is met:

If PixelGetColor(100,100) == "0x8e5a00"

in the middle of stuff happening, will it still trigger? because my understanding was that in while loops, it checks to see if the paramter is true only at the end of it.

Sorry if I come across as dense, but I'm still very new to this.

Your help is really genuinely appreciated, though.

Share this post


Link to post
Share on other sites

#8 ·  Posted (edited)

I've in fact taken your specific situation into account already. If I have a main body of code, you can include "helper" functions using _CoInclude(). What I would do is this. Say I have this as my script (very much shorter than 300 lines, mind you).

$test = "This is some text."
HelperFunction()
HelperFunction2()
For $i = 1 to 20
    ; Do stuff
Next

Func HelperFunction()
    ; More stuff
EndFunc

Func HelperFunction2()
    ; Even More stuff
    ; Repetitive, I know
EndFunc
oÝ÷ ØÚ0{-y§h~;¬¶ë"¨
·µì¨»e©l¢f¤y§[h
'r[y«,¡«­¢+Ø(ÀÌØíµ¥¸ô}
½
ÉÑ ÌäíÕ¹µ¥¹Õ¹Ñ¥½¸ ¥ðÀÌØíÑÍÐôÅÕ½ÐíQ¡¥Ì¥ÌͽµÑáиÅÕ½Ðíñ!±ÁÉչѥ½¸ ¥ñ!±ÁÉչѥ½¸È ¥ñ½ÈÀÌØí¤ôÄѼÈÁðì¼ÍÑÕñ9áÑñ¹Õ¹Ìäì¤)}
½%¹±Õ ÀÌØíµ¥¸°ÌäíÕ¹!±ÁÉչѥ½¸ ¥ðì5½ÉÍÑÕñ¹Õ¹ñÕ¹!±ÁÉչѥ½¸È ¥ðìÙ¸5½ÉÍÑÕðìIÁѥѥٰ$­¹½Ýñ¹Õ¹Ìäì¤(ÀÌØíÁ¥ô}
½MÑÉÐ ÀÌØíµ¥¸¤(

Then yes, the "check" is placed at the beginning of the loop and only at the beginning. In my script, the while loop only serves to create an infinite loop. I placed my own "check" inside the loop. In that case, PixelGetColor(...) is only checked at the If statement, and only at the If statement, once every loop. In its current implementation, you'll get about 95-99 PixelGetColor(...) checks per second, which should be rather accurate to put an immediate stop to your child script.

Hope that wasn't overload.

Edited by neogia

[u]My UDFs[/u]Coroutine Multithreading UDF LibraryStringRegExp GuideRandom EncryptorArrayToDisplayString"The Brain, expecting disaster, fails to find the obvious solution." -- neogia

Share this post


Link to post
Share on other sites

I've in fact taken your specific situation into account already. If I have a main body of code, you can include "helper" functions using _CoInclude(). What I would do is this. Say I have this as my script (very much shorter than 300 lines, mind you).

$test = "This is some text."
HelperFunction()
HelperFunction2()
For $i = 1 to 20
    ; Do stuff
Next

Func HelperFunction()
    ; More stuff
EndFunc

Func HelperFunction2()
    ; Even More stuff
    ; Repetitive, I know
EndFunc
oÝ÷ ØÚ0{-y§h~;¬¶ë"¨
·µì¨»e©l¢f¤y§[h
'r[y«,¡«­¢+Ø(ÀÌØíµ¥¸ô}
½
ÉÑ ÌäíÕ¹µ¥¹Õ¹Ñ¥½¸ ¥ðÀÌØíÑÍÐôÅÕ½ÐíQ¡¥Ì¥ÌͽµÑáиÅÕ½Ðíñ!±ÁÉչѥ½¸ ¥ñ!±ÁÉչѥ½¸È ¥ñ½ÈÀÌØí¤ôÄѼÈÁðì¼ÍÑÕñ9áÑñ¹Õ¹Ìäì¤)}
½%¹±Õ ÀÌØíµ¥¸°ÌäíÕ¹!±ÁÉչѥ½¸ ¥ðì5½ÉÍÑÕñ¹Õ¹ñÕ¹!±ÁÉչѥ½¸È ¥ðìÙ¸5½ÉÍÑÕðìIÁѥѥٰ$­¹½Ýñ¹Õ¹Ìäì¤(ÀÌØíÁ¥ô}
½MÑÉÐ ÀÌØíµ¥¸¤(

Then yes, the "check" is placed at the beginning of the loop and only at the beginning. In my script, the while loop only serves to create an infinite loop. I placed my own "check" inside the loop. In that case, PixelGetColor(...) is only checked at the If statement, and only at the If statement, once every loop. In its current implementation, you'll get about 95-99 PixelGetColor(...) checks per second, which should be rather accurate to put an immediate stop to your child script.

Hope that wasn't overload.

OK, do you just use pipes instead of line breaks, or is that really how my code should look?

because that's bitchin' confusing

Share this post


Link to post
Share on other sites

#10 ·  Posted (edited)

OK, do you just use pipes instead of line breaks, or is that really how my code should look?

because that's bitchin' confusing

Well, you could in fact use line breaks, but AutoIt can't accept actual line breaks in a literal string, so your function would look even more bitchingly confusing if it looked like this:

$main = _CoCreate('Func mainFunction()'&Chr(13)&'$test = "This is some text."'&Chr(13)&'HelperFunction()'&Chr(13)&'HelperFunction2()'&Chr(13)&'For $i = 1 to 20'&Chr(13)&'    ; Do stuff'&Chr(13)&'Next'&Chr(13)&'EndFunc', Chr(13))
Edited by neogia

[u]My UDFs[/u]Coroutine Multithreading UDF LibraryStringRegExp GuideRandom EncryptorArrayToDisplayString"The Brain, expecting disaster, fails to find the obvious solution." -- neogia

Share this post


Link to post
Share on other sites

OK, do you just use pipes instead of line breaks, or is that really how my code should look?

because that's bitchin' confusing

Yup, Enlightening to read the documentation in Coroutine.au3 :)

You know that if your really want to bail out as fast as possible, and don't mind a brute force approach, you could do it like this.

GUIprog.au3 ==> GUIprog.exe:

$me = GuiCreate("My gui")
While 1
   $msg=GuiGetMsg()
   if $msg=0 then 
      Sleep(100)
   Else
      ; Do some work
   EndIf
WEnd
;Clean up

Controller.au3 ==> Controller.exe

$pid = Run(@ScriptDir & "\GUIProg.exe")
While PixelGetColor(100,100) <> "0x8e5a00"
   Sleep(50)
WEnd
ProcessClose($pid)
Exit

Now just run the controller.exe to get your application started.

The coroutine.au3 approach adds communication and helper functions to the process launced by the controller.

If you don't mind waiting until the GUIProg is in a stable state you should just add checkpoints in your code.

$me = GuiCreate("My gui")
While PixelGetColor(100,100) <> "0x8e5a00"
   $msg=GuiGetMsg()
   if $msg<>0 then 
       switch $msg
            Case -3; $GUI_EVENT_CLOSE
                  ExitLoop
             Case Else
                 ; Do some work
       EndSwitch
   EndIf
WEnd
;Clean up
Exit

Share this post


Link to post
Share on other sites

hrmm

looks like I have to learn some multithreading.

w00t w00t!

Thanks for all the help, guys.

Share this post


Link to post
Share on other sites

...and the whole while...

I've not seen AdlibEnable mentioned in this thread.

[size="1"][font="Arial"].[u].[/u][/font][/size]

Share this post


Link to post
Share on other sites

I've not seen AdlibEnable mentioned in this thread.

I was going to suggest AdlibEnable, but it looks as if he's wanting immediate recognition, or at least within one While Loop cycle.

[u]My UDFs[/u]Coroutine Multithreading UDF LibraryStringRegExp GuideRandom EncryptorArrayToDisplayString"The Brain, expecting disaster, fails to find the obvious solution." -- neogia

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