stevey Posted April 5, 2007 Share Posted April 5, 2007 (edited) Is there a way to loop a function or some statements the entire time a program is running? Like while the program is doing its own thing, can a func be setup to always keep looping? or would I have to keep calling that function and alternating Edited April 5, 2007 by stevey Link to comment Share on other sites More sharing options...
anixon Posted April 5, 2007 Share Posted April 5, 2007 Is there a way to loop a function or some statements the entire time a program is running?Yes you probably will use a While statementWhile 1 $dll = DllOpen("user32.dll") Sleep(50) Run(@ScriptDir & "\your process.exe") If _IsPressed('14', $dll) And _IsPressed('1B', $dll) Then ; Caps + Esc Close Keys Monitor DllClose($dll) exit EndIf ;Another process or If Statement ;Another process or If Statement ;AdinfinatiumWEndAnt.. Link to comment Share on other sites More sharing options...
stevey Posted April 5, 2007 Author Share Posted April 5, 2007 (edited) So, are you saying something like this...? While 1 Run(@ScriptDir & "program.exe") If $condition = True Then ;blah ;blah EndIf WEnd Isn't that the same as While 1 Program() If $condition = True Then ;blah ;blah EndIf WEnd Func Program() ;program here EndFunc I need to in a way be running two programs at once that know what each are doing.... I don't think that is the best way to describe it.. I basically have a program that needs to check for new windows all the time while doing other stuff..... Edited April 5, 2007 by stevey Link to comment Share on other sites More sharing options...
therks Posted April 5, 2007 Share Posted April 5, 2007 You could try AdlibEnable, or running two scripts at once. Chances are though, if you manage it right, you could probably do it in a while loop. We would really be able to help you more if we knew a little more about what you're doing. Hint, hint, post some code. My AutoIt Stuff | My Github Link to comment Share on other sites More sharing options...
Uten Posted April 5, 2007 Share Posted April 5, 2007 Is there a way to loop a function or some statements the entire time a program is running? Like while the program is doing its own thing, can a func be setup to always keep looping? or would I have to keep calling that function and alternatingThis would require multithreading, if I understand your request right. Take a look at the alternative approaches used in AutoIt does not support multiple threads. Communication between two processes is your best bet if you need this in "Autoit only code". Take a look at my signature or browse Scripts for solutions. If you don't really need the loop in your function to run tight you can use byref calls to your function to save and restore internal state and then call the function from your main loop. Ex (from the top of my head: Func Looping(ByRef $arg1, ByRef $arg2) If $arg2 = 0 then $arg2 = TimeInit() $arg1 += 1 $arg2 = TimeDiff($arg2) EndFunc ;Usin a function to encapsulate main code and declarations Func Main() Local $arg1, $arg2 ;Main loop While GuiGetMsg() <> -3 Looping($arg1, $arg2) ConsoleWrite("(" & @ScriptLineNumber & ") := " & "$arg1:=" & $arg1 & ", $arg2:=" & $arg2 & @CRLF) Sleep(133) WEnd EndFunc Main() Please keep your sig. small! Use the help file. Search the forum. Then ask unresolved questions :) Script plugin demo, Simple Trace udf, TrayMenuEx udf, IOChatter demo, freebasic multithreaded dll sample, PostMessage, Aspell, Code profiling Link to comment Share on other sites More sharing options...
stevey Posted April 6, 2007 Author Share Posted April 6, 2007 I am using it to check for new windows that have popped and if there is a window then to close it... Is there anything wrong with using the AdlibEnable to loop that function to close windows every second or two? Or is there a better method? Like this: AdlibEnable("CheckForWindows", 1000) While 1 ;program looping here WEnd Func CheckForWindows() ;if statement for window ;another if statement EndFunc Link to comment Share on other sites More sharing options...
therks Posted April 6, 2007 Share Posted April 6, 2007 Why can't you just put that in the while loop? My AutoIt Stuff | My Github Link to comment Share on other sites More sharing options...
stevey Posted April 6, 2007 Author Share Posted April 6, 2007 Because, my main program is lengthy and if it is put in the while loop, then it will only loop after each rotation of the program... Rather than checking for windows every so often.. because my main program might take anywhere from a few seconds to 10 minutes or longer Unless I am confused Link to comment Share on other sites More sharing options...
anixon Posted April 6, 2007 Share Posted April 6, 2007 Because, my main program is lengthy and if it is put in the while loop, then it will only loop after each rotation of the program... Rather than checking for windows every so often.. because my main program might take anywhere from a few seconds to 10 minutes or longerUnless I am confusedMany ways to solve this problem. One is to have a seperate loop program write to an ini file that xyz is happening and then in the main program you could have it read the ini file as many times as you like to make sure that the flag is set to true or falseThis is an ini write statement that sets a 0 flag which has been encrypted;Encryption Processing;#include<String.au3> $key0 = (_StringEncrypt(1, "0", "yourencryptionkey", 1))IniWrite(@ScriptDir & "\inifiles\yourinifile.ini", "section1", "value1", $key0)and to read the file$readflag = (_StringEncrypt(0, IniRead(@ScriptDir & "\inifiles\yourinifile.ini", "section1", "value1", ""), "yourencryptionkey", 1))and this would set a 1 flag$key1 = (_StringEncrypt(1, "1", "yourencryptionkey", 1))IniWrite(@ScriptDir & "\inifiles\yourinifile.ini", "section1", "value1", $key1)Then in your main program$readflag = (_StringEncrypt(0, IniRead(@ScriptDir & "\inifiles\yourinifile.ini", "section1", "value1", ""), "yourencryptionkey", 1))If $readflag = 1 then ;do this processElse ;do this processEndifYou need the read the file every time before the test to make sure that the value has not changed from 0 to 1 since the last readI use this technique in certain circumstances.Alternatively you can check and make sure that the application is still running like as followsFunc checkitshappening() Sleep(50) $PID = ProcessExists($filename) If $PID = 0 Then $key0 = (_StringEncrypt(1, "0", "yourencryptionkey", 1)) IniWrite(@ScriptDir & "\inifiles\yourinifile.ini", "section1", "value1", $key0) exit else $key1 = (_StringEncrypt(1, "1", "yourencryptionkey", 1)) IniWrite(@ScriptDir & "\inifiles\yourinifile.ini", "section1", "value1", $key1) endifendfuncyour programWhile 1 do a process checkitshappening() do a process checkitshappening() do a process checkitshappening()wendso on and so forth and you dont have to write to an ini file you can simply set a global value global $flag = 0 If xyz then $flag = 0 else $flag = 1 endifyour progamWhile 1 if $flag = 1 then do this else do this endif continue your process if $flag = 1 then do this else do this endif continue your processwendHope that is of some help and I am sure that the experts have a better solution but as a learner with six months experience I Am trying to give something back as my contributionCheers Ant... Link to comment Share on other sites More sharing options...
stevey Posted April 6, 2007 Author Share Posted April 6, 2007 (edited) So what is different then all of that and AdlibEnable() in my situation? Edited April 6, 2007 by stevey Link to comment Share on other sites More sharing options...
Uten Posted April 6, 2007 Share Posted April 6, 2007 Addlib can be blocked by another lengthy operation. AdlibEnable("trigger", 1000) $i = 5 Do $i += $i/2 - $i/3 Until 1==2 Func trigger() ConsoleWrite("Test" & @CRLF) EndFunc hmm, this seems to have been fixed since I last tested it. So, never mind.... Please keep your sig. small! Use the help file. Search the forum. Then ask unresolved questions :) Script plugin demo, Simple Trace udf, TrayMenuEx udf, IOChatter demo, freebasic multithreaded dll sample, PostMessage, Aspell, Code profiling Link to comment Share on other sites More sharing options...
therks Posted April 7, 2007 Share Posted April 7, 2007 The main problem I personally have with Adlib is that you can only have one at a time. So if a UDF happened to use Adlib (which is bad form really, but it could happen) it will cancel out your Adlib. Personally I wish that AutoIt could have something like Javascript's setTimeout, or setInterval functions. My AutoIt Stuff | My Github Link to comment Share on other sites More sharing options...
Richard Robertson Posted April 7, 2007 Share Posted April 7, 2007 If all the second "loop" is doing is checking for windows and closing them, why do the two need to communicate? Link to comment Share on other sites More sharing options...
stevey Posted April 7, 2007 Author Share Posted April 7, 2007 They only have to communicate on one certain window if it pops up, other then that, they never have to Link to comment Share on other sites More sharing options...
Uten Posted April 7, 2007 Share Posted April 7, 2007 Then create two applications and let the two communicate with a flag ( mutex, registry or ini file) . Please keep your sig. small! Use the help file. Search the forum. Then ask unresolved questions :) Script plugin demo, Simple Trace udf, TrayMenuEx udf, IOChatter demo, freebasic multithreaded dll sample, PostMessage, Aspell, Code profiling Link to comment Share on other sites More sharing options...
stevey Posted April 7, 2007 Author Share Posted April 7, 2007 (edited) Ok... so my question is what does any of those have to offer over AdlibEnable() in my situation? Edited April 7, 2007 by stevey Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now