Jump to content

Looping a function always while a program is running?


Recommended Posts

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 by stevey
Link to comment
Share on other sites

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 statement

While 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

;Adinfinatium

WEnd

Ant..

Link to comment
Share on other sites

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 by stevey
Link to comment
Share on other sites

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

This 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()
Link to comment
Share on other sites

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

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

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

Many 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 false

This 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 process

Else

;do this process

Endif

You 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 read

I use this technique in certain circumstances.

Alternatively you can check and make sure that the application is still running like as follows

Func 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)

endif

endfunc

your program

While 1

do a process

checkitshappening()

do a process

checkitshappening()

do a process

checkitshappening()

wend

so 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

endif

your progam

While 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 process

wend

Hope 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 contribution

Cheers Ant...

Link to comment
Share on other sites

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. :shocked:

So, never mind....:(

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

Then create two applications and let the two communicate with a flag ( mutex, registry or ini file) .

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

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...