Jump to content

Run 2 loops at once


Recommended Posts

IS it possible to run 2 loops at once?

I want run this code constantly

$Mem_Open = _MemoryOpen($Process1) ;must open before you can read address
   $Mem_Read = _MemoryRead($Mem_Address, $Mem_Open) ;reads value at memory address
   _MemoryClose($Mem_Open) ;close it afterwards
  
   GUICtrlSetData($Label_1,$Mem_Read) ; sets label to value of read memory

whilst at the same time having another loop checking the var $Mem_Read and do various things depending on the value.

Is this possible?

Link to comment
Share on other sites

IS it possible to run 2 loops at once?

I want run this code constantly

$Mem_Open = _MemoryOpen($Process1) ;must open before you can read address
$Mem_Read = _MemoryRead($Mem_Address, $Mem_Open) ;reads value at memory address
_MemoryClose($Mem_Open) ;close it afterwards

GUICtrlSetData($Label_1,$Mem_Read) ; sets label to value of read memory

whilst at the same time having another loop checking the var $Mem_Read and do various things depending on the value.

Is this possible?

Simple answer is no I think.

As you probably already know, AutoIt isn't multi-threaded and the only way to do something like that is using another language that can multi-thread or runnig another process and syncing stuff just right and having the communication between them down.

you can try writing a dll to spawn another thread and do some basic stuff and communicate with autoits thread.

But as for running two For Next Loops, it's out of the question :P

Global Static $Struc = "wchar Msg[10];wchar Msg2[10];int switch"
Global $CallBack = DllCallbackRegister("_test", "int", "ptr")
Global $Params = DllStructCreate($Struc)

DllStructSetData($Params, "Msg", "Test1")
DllStructSetData($Params, "switch", 1)
Make($Params)
Sleep(1); give the thead time to get its info

DllStructSetData($Params, "Msg2", "Test2")
DllStructSetData($Params, "switch", 2)
Make($Params)

Sleep(3000)

Func _test($pointer)

    Local $New = DllStructCreate($Struc, $pointer)
    Local $Text

    Switch Int(DllStructGetData($New, "switch"))
        Case 1
            $Text = DllStructGetData($New, "Msg")
        Case 2
            $Text = DllStructGetData($New, "Msg2")
    EndSwitch

    For $i = 0 to 100
        Sleep(10)
        ConsoleWrite($Text & " " & $I & @CR)
    Next

    Return 1

EndFunc

Func Make($Text)
    Local $hThread = DllCall("kernel32.dll", "hwnd" ,"CreateThread" , _
        "ptr", 0, _
        "dword", 0, _
        "long", DllCallbackGetPtr($CallBack), _
        "ptr", DllStructGetPtr($Params), _
        "long", 0, _
        "int*", 0 _
    )
    If @error Then Return SetError(1, 0, 0)
    Return $hThread[0]
EndFunc

that code came from some one who posted something just like it somewhere here trying to show why it doesn't work.

I've played with it more than I should have and figured that it's a lost cause thing.

Although, using a lower lever language to make threads and help autoit with some things seems to work well. But I'm not 100% sure though because I've still been messing with that.

Edited by DeputyDerp
Link to comment
Share on other sites

Yes, see the help file for adlibregister

kylomas

But adlib regster calls the function after any other function has freed time to execute the instructions, it's not executing the loops at the same time.

It all really depends on what he asked, because if he means like running to loops like really at the same time, than that's impossible in autoit, he wouldn't even need adlib register in a case like the example he posted because it would be better to just check the information from his varable exactly right after it was set by whatever function set it.

Link to comment
Share on other sites

I suppose you can do TCP connections between two independent scripts. Where one listens and acts, other posts.

Or, again, two scripts, where one writes to a file, and the other waits for the file, and reads it to do some action.

Edited by jdelaney
IEbyXPATH-Grab IE DOM objects by XPATH IEscriptRecord-Makings of an IE script recorder ExcelFromXML-Create Excel docs without excel installed GetAllWindowControls-Output all control data on a given window.
Link to comment
Share on other sites

I suppose you can do TCP connections between two independent scripts. Where one listens and acts, other posts.

Or, again, two scripts, where one writes to a file, and the other waits for the file, and reads it to do some action.

that too, but I think it's easier to just use structures, get the first thread to make a structure and then pass the pointer to the other script and have one of the scripts set it's information to tell the other one what to do.

Link to comment
Share on other sites

Why do you want to solve your problem with a solution using 2 loops?

Can it not just be done in 1 loop? The example you gave looks to be solvable with 1 loop.

Another function that maybe can help _Timer_SetTimer to check something every n milliseconds

Link to comment
Share on other sites

I duno what ur trying to do but I's starting to think that whatever it is, you might not need to do that...

say you have your code and you want it to be checking whatver its checking

while 1
    $Mem_Open = _MemoryOpen($Process1) ;must open before you can read address
    $Mem_Read = _MemoryRead($Mem_Address, $Mem_Open) ;reads value at memory address
    _MemoryClose($Mem_Open) ;close it afterwards

    GUICtrlSetData($Label_1,$Mem_Read) ; sets label to value of read memory
wend

and you want another loop to check that mem_read variable then i don't think you need a seperate loop for whatever it is you wana do.

just put somehing in there to check it right away.

while 1
    $Mem_Open = _MemoryOpen($Process1) ;must open before you can read address
    $Mem_Read = _MemoryRead($Mem_Address, $Mem_Open) ;reads value at memory address
    _MemoryClose($Mem_Open) ;close it afterwards

    GUICtrlSetData($Label_1,$Mem_Read) ; sets label to value of read memory
    if $Mem_Read = whatevr then
        _something_that_does_things($Mem_Read)
        exitloop
    endif
wend
;continue with whatever

you probably need to explain more what your doing.

Link to comment
Share on other sites

@deputyderp

But adlib regster calls the function after any other function has freed time to execute the instructions, it's not executing the loops at the same time.

Of course, consider what the OP wants to do. Do you really think that he is talking about synchronous execution?

Adlib's are a natural for what he wants to do, and, extremely easy to code.

@noobcube - here's a sample of what an adlib looks like. The function "autosave" runs evrey 5 seconds, regardless of what the script is doing. It is NOT concurrent execution, but for what you are doing it will seem like it.

#include <StaticConstants.au3>
#include <GUIConstantsEx.au3>
#include <date.au3>

#AutoIt3Wrapper_Add_Constants=n

local $gui010   =   guicreate('ADLIB Example Script')
local $aSize    =   wingetclientsize($gui010)
local $edt010   =   guictrlcreateedit('',10,10,$asize[0]-20,$asize[1]-80)
local $btn010   =   guictrlcreatebutton('Stop AutoSave',10,$asize[1]-60,$asize[0]-20)
                    guictrlsetcolor(-1,0x0000ff)
                    guictrlsetfont(-1,10,800)
local $status   =   guictrlcreatelabel('',10,$asize[1]-30,$asize[0]-20,20,$ss_sunken)
                    guictrlsetfont(-1,8.5,800,-1,'Lucinda Console')
                    guictrlsetcolor(-1,0xaa0000)
                    guisetstate()

adlibregister('AutoSave',5000)

While 1
    switch GUIGetMsg()
        case $GUI_EVENT_CLOSE
            Exit
        case $btn010
            if guictrlread($btn010) = 'Stop AutoSave' then                  ; check button text
                adlibunregister('AutoSave')                                 ; stop adlib
                guictrlsetdata($btn010,'Start AutoSave')                    ; set button text to 'start'
                guictrlsetdata($status,'AutoSave Stopped at ' & _now())     ; report status
            else
                adlibregister('AutoSave',3000)                              ; button controltext was 'start' so register adlib
                guictrlsetdata($btn010,'Stop AutoSave')                     ; toggle button control text
                guictrlsetdata($status,'AutoSave Started at ' & _now())     ; report status
            endif
            guictrlsetstate($edt010,$gui_focus)                             ; return focus to edit control
    EndSwitch
wend

func Autosave()

    local $fln = @scriptdir & '\edit_auto_save.txt'
    local $hfl = fileopen($fln,2)
    if $hfl = -1 then
        guictrlsetdata($status,'AutoSave File Failed to Open')
        Return
    Else
        guictrlsetdata($status,'Last AutoSave at ' & _now())
    EndIf
    filewrite($hfl,guictrlread($edt010))
    fileclose($hfl)
    $hfl = 0

endfunc

kylomas

edit: spelling

Edited by kylomas

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Link to comment
Share on other sites

noobcube,

Here's another, simpler example. You can do stuff in the message loop while the adlib is running.

; *** Start added by AutoIt3Wrapper ***
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
; *** End added by AutoIt3Wrapper ***
;
;
;

#AutoIt3Wrapper_Add_Constants=n

local $myvar = @sec

adlibregister("check_var",1000)

local $gui010 = guicreate('Adlib Example',200,200)
local $edt010 = guictrlcreateedit('',0,0,200,150,$es_readonly)
local $btn010 = guictrlcreatebutton('My Button',0,170,200,20)
                guisetstate()

while 1

    ; the message loop runs while the adlib is running

    switch guigetmsg()
        case $gui_event_close
            exit
        case $btn010
            guictrlsetdata($edt010,'Button pressed' & @crlf,1)

            ; or whatever else you want to do

    endswitch

wend

func check_var()
    if @sec <> $myvar then
        guictrlsetdata($edt010,@hour & ':' & @min & ':' & @sec & @crlf,1)
        $myvar = @SEC
    endif
endfunc

kylomas

edit: in case you have'nt gotten it yet, run your memory routine inside the adlib. Just make sure that the adlib has visibility to your variables by declaring them in "global" scope.

Edited by kylomas

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

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