Jump to content

Multithreading Question


Recommended Posts

So I guess I am having problems understanding how multi-threading is actually happening as far as the program stepping is concerned. I am trying to launch a multi-thread based image search to help speed things up, and the first image search works great. The part I am stuck in is running the functions again to search for a different function. So basically, I want all four threads to search for picture 1 first, close those threads, then wait one second, then re-open and search for picture2, and on and on..

 

 

#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_Run_Tidy=y
#AutoIt3Wrapper_Run_Au3Stripper=y
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****
#include 'authread.au3'
#include 'ImageSearch2015.au3'

$left = 0
$top = 0
$right = @DesktopWidth
$bottom = @DesktopHeight
$pic1 = "SampleTableButton.png"
$pic2 = "SampleTableSaveAsButton.png"
$sleeptimer = 1000


_AuThread_Startup()

$threadA = _AuThread_StartThread("cA")
_AuThread_SendMessage($threadA, $pic1)
$threadB = _AuThread_StartThread("cB")
_AuThread_SendMessage($threadB, $pic1)
$threadC = _AuThread_StartThread("cC")
_AuThread_SendMessage($threadC, $pic1)
$threadD = _AuThread_StartThread("cD")
_AuThread_SendMessage($threadD, $pic1)
_AuThread_CloseThread("cA")
_AuThread_CloseThread("cB")
_AuThread_CloseThread("cC")
_AuThread_CloseThread("cD")
Sleep(1000)

$threadA = _AuThread_StartThread("cA")
_AuThread_SendMessage($threadA, $pic2)
$threadB = _AuThread_StartThread("cB")
_AuThread_SendMessage($threadB, $pic2)
$threadC = _AuThread_StartThread("cC")
_AuThread_SendMessage($threadC, $pic2)
$threadD = _AuThread_StartThread("cD")
_AuThread_SendMessage($threadD, $pic2)
_AuThread_CloseThread("cA")
_AuThread_CloseThread("cB")
_AuThread_CloseThread("cC")
_AuThread_CloseThread("cD")

Func cA()
    While True
        $picture = _AuThread_GetMessage()

        $x1 = $left
        $y1 = $top
        $x2 = ($right - $left) / 2
        $y2 = ($bottom - $top) / 2

        $returnX = 0
        $returnY = 0

        $result = _ImageSearchArea($picture, 1, $x1, $y1, $x2, $y2, $returnX, $returnY, 0, 0)

        If $result = 1 Then
            ToolTip("Yay! We found the image in Coord A:" & $returnX & " " & $returnY, $x1, $y1)
            MouseClick("Left", $returnX, $returnY, 1, 2)
            Sleep(5000)
        EndIf

        Sleep(1000)
    WEnd
EndFunc   ;==>cA

Func cB()
    While True
        $picture = _AuThread_GetMessage()
        $x1 = $left
        $y1 = ($bottom - $top) / 2
        $x2 = ($right - $left) / 2
        $y2 = $bottom

        $returnX = 0
        $returnY = 0

        $result = _ImageSearchArea($picture, 1, $x1, $y1, $x2, $y2, $returnX, $returnY, 0, 0)

        If $result = 1 Then
            ToolTip("Yay! We found the image in Coord B:" & $returnX & " " & $returnY, $x1, $y1)
            MouseClick("Left", $returnX, $returnY, 1, 2)
        EndIf
    WEnd
EndFunc   ;==>cB
Func cC()
    While True
        $picture = _AuThread_GetMessage()
        $x1 = ($right - $left) / 2
        $y1 = $top
        $x2 = $right
        $y2 = ($bottom - $top) / 2

        $returnX = 0
        $returnY = 0

        $result = _ImageSearchArea($picture, 1, $x1, $y1, $x2, $y2, $returnX, $returnY, 0, 0)

        If $result = 1 Then
            ToolTip("Yay! We found the image in Coord C:" & $returnX & " " & $returnY, $x1, $y1)
            MouseClick("Left", $returnX, $returnY, 1, 2)
        EndIf
    WEnd
EndFunc   ;==>cC
Func cD()
    While True
        $picture = _AuThread_GetMessage()
        $x1 = ($right - $left) / 2
        $y1 = ($bottom - $top) / 2
        $x2 = $right
        $y2 = $bottom

        $returnX = 0
        $returnY = 0

        $result = _ImageSearchArea($picture, 1, $x1, $y1, $x2, $y2, $returnX, $returnY, 0, 0)

        If $result = 1 Then
            ToolTip("Yay! We found the image in Coord D:" & $returnX & " " & $returnY, $x1, $y1)
            MouseClick("Left", $returnX, $returnY, 1, 2)
        EndIf
    WEnd
EndFunc   ;==>cD

 

Edited by BatMan22
Link to comment
Share on other sites

2 hours ago, BatMan22 said:

Lol.. that's where I stole my code from. It works, great. It's a matter of how to restart the threads to look for a different picture :) That's where I am stuck

From what I gather i think you need to basically get the callback from the thread when it finds the picture and then switch the picture in the function that its running if that makes any sense. 

 

Edit... you may want to check that the autoit program is actually exiting the while loops.  As far as I can tell true is always going to be true.  If it finds the picture add in an exit loop to get out of there

Edited by markyrocks
Link to comment
Share on other sites

after studying the examples and trying to understand what how the thing works you need to basically have a main while loop that holds the main thread open indefinitely.  then use the thread starter calls to call the other functions.  but they will need to exit at somepoint so that different thread starter calls can be made. i've played with it a little and wasn't very successful but it does appear to work.  but you definitely need a main while loop. the child loops need to exit after the objective has been achieved tho.  maybe thats not right either but thats the way the example shows.  

Edited by markyrocks
Link to comment
Share on other sites

46 minutes ago, markyrocks said:

after studying the examples and trying to understand what how the thing works you need to basically have a main while loop that holds the main thread open indefinitely.  then use the thread starter calls to call the other functions.  but they will need to exit at somepoint so that different thread starter calls can be made. i've played with it a little and wasn't very successful but it does appear to work.  but you definitely need a main while loop. the child loops need to exit after the objective has been achieved tho.  maybe thats not right either but thats the way the example shows.   

Kickass. You're right. I don't REALLY understand why a loop is required in main, but that fixed it for me. Pauses in the RIGHT AREA, will also allow for multiple searches.. it's SUPER weird. See my code below for my experience:

#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_Run_Tidy=y
#AutoIt3Wrapper_Run_Au3Stripper=y
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****
#include 'authread.au3'
#include 'ImageSearch2015.au3'

$left = 0
$top = 0
$right = @DesktopWidth
$bottom = @DesktopHeight
$pic1 = "SampleTableButton.png"
$pic2 = "SampleTableSaveAsButton.png"
$sleeptimer = 1000


_AuThread_Startup()

$threadA = _AuThread_StartThread("cA")
$threadB = _AuThread_StartThread("cB")
$threadC = _AuThread_StartThread("cC")
$threadD = _AuThread_StartThread("cD")


While 1 ; If I put a loop in, then both searches will complete infinitely and successfully.
;~ Sleep(1000) ; If I put a sleep here, then the first and only first imagesearch will complete
_AuThread_SendMessage($threadA, $pic1)
_AuThread_SendMessage($threadB, $pic1)
_AuThread_SendMessage($threadC, $pic1)
_AuThread_SendMessage($threadD, $pic1)
;~ Sleep(1000) ; If I put a sleep here, then the both image searches will complete
_AuThread_SendMessage($threadA, $pic2)
_AuThread_SendMessage($threadB, $pic2)
_AuThread_SendMessage($threadC, $pic2)
_AuThread_SendMessage($threadD, $pic2)
;~ Sleep(1000) ; If I put a sleep here, then NEITHER image search will complete
If WinExists("Save sample table") Then Exit
WEnd

Func cA()
    While True
        $picture = _AuThread_GetMessage()

        $x1 = $left
        $y1 = $top
        $x2 = ($right - $left) / 2
        $y2 = ($bottom - $top) / 2

        $returnX = 0
        $returnY = 0

        $result = _ImageSearchArea($picture, 1, $x1, $y1, $x2, $y2, $returnX, $returnY, 0, 0)

        If $result = 1 Then
            ToolTip("Yay! We found the image in Coord A:" & $returnX & " " & $returnY, $x1, $y1)
            MouseClick("Left", $returnX, $returnY, 1, 2)
;~          Sleep(5000)
        EndIf

        Sleep(1000)
    WEnd
EndFunc   ;==>cA

Func cB()
    While True
        $picture = _AuThread_GetMessage()
        $x1 = $left
        $y1 = ($bottom - $top) / 2
        $x2 = ($right - $left) / 2
        $y2 = $bottom

        $returnX = 0
        $returnY = 0

        $result = _ImageSearchArea($picture, 1, $x1, $y1, $x2, $y2, $returnX, $returnY, 0, 0)

        If $result = 1 Then
            ToolTip("Yay! We found the image in Coord B:" & $returnX & " " & $returnY, $x1, $y1)
            MouseClick("Left", $returnX, $returnY, 1, 2)
        EndIf
    WEnd
EndFunc   ;==>cB
Func cC()
    While True
        $picture = _AuThread_GetMessage()
        $x1 = ($right - $left) / 2
        $y1 = $top
        $x2 = $right
        $y2 = ($bottom - $top) / 2

        $returnX = 0
        $returnY = 0

        $result = _ImageSearchArea($picture, 1, $x1, $y1, $x2, $y2, $returnX, $returnY, 0, 0)

        If $result = 1 Then
            ToolTip("Yay! We found the image in Coord C:" & $returnX & " " & $returnY, $x1, $y1)
            MouseClick("Left", $returnX, $returnY, 1, 2)
        EndIf
    WEnd
EndFunc   ;==>cC
Func cD()
    While True
        $picture = _AuThread_GetMessage()
        $x1 = ($right - $left) / 2
        $y1 = ($bottom - $top) / 2
        $x2 = $right
        $y2 = $bottom

        $returnX = 0
        $returnY = 0

        $result = _ImageSearchArea($picture, 1, $x1, $y1, $x2, $y2, $returnX, $returnY, 0, 0)

        If $result = 1 Then
            ToolTip("Yay! We found the image in Coord D:" & $returnX & " " & $returnY, $x1, $y1)
            MouseClick("Left", $returnX, $returnY, 1, 2)
        EndIf
    WEnd
EndFunc   ;==>cD

 

 

Link to comment
Share on other sites

glad i could help you out!! I'm interested in this authread package aswell and i honestly thought it was broken until i copied the example an ran it and was like huh i guess it does work and then compared what i was doing to the example and the only difference was the main while loop.... don't ask me why that matters but clearly it does.

Link to comment
Share on other sites

28 minutes ago, markyrocks said:

glad i could help you out!! I'm interested in this authread package aswell and i honestly thought it was broken until i copied the example an ran it and was like huh i guess it does work and then compared what i was doing to the example and the only difference was the main while loop.... don't ask me why that matters but clearly it does. 

So it seems a bit buggy, or maybe I am failing to understand it completely.. but the way I understand it is this..

The Main program runs, and calls the threads. If your MAIN program takes 10 seconds to run, and your threads take 20seconds to run, then your threads will be cut short at 10 seconds and will be shutdown. The part that really confuses me is the messaging and the 'thread start'.. Seems like the threads NEED loops in order to give them time to receive messages from the main thread.ie. You can't start a thread with an argument attached like _AuThread_StartThread("Nameofthread", "NameOfFirstPicture") which is what makes the messaging necessary and the timing to be an issue/annoying.

Edited by BatMan22
Link to comment
Share on other sites

3 hours ago, BatMan22 said:

So it seems a bit buggy, or maybe I am failing to understand it completely.. but the way I understand it is this..

The Main program runs, and calls the threads. If your MAIN program takes 10 seconds to run, and your threads take 20seconds to run, then your threads will be cut short at 10 seconds and will be shutdown. The part that really confuses me is the messaging and the 'thread start'.. Seems like the threads loop an infinite amount of times even if you don't have a loop in them.

ok so the first suggestion that i have for you is try simplifying your existing code until you get the desired behavior and then build up from there.

My second suggestion is that you should familiarize yourself with like gui msgs and how they work bc this doesn't seem that different.  you are using a message to pass a picture like a variable into a function.    Think of messages like an event.  like you have a loop that supposed to count to 10 once it gets to 10 it sends up a flag like "hey i counted to 10".  The point of this message is to trigger the next series of events.    that in mind you have messages being sent from the main while loop that are not being triggered by any particular event.  how or why or when they're being sent is completely up to time delays and happenstance.   So the idea is in the main something happened it sends a message to a sub thread to do something bc of a previous event etc.  again you have sent and received msg values going into parameters for a function.  that function is startin b4 the message is received so whatever the default value of AuThread_GetMsg() is being passed (Probably Null) to the imagesearch function and probably resulting in a error or causing other unpredictable behavior.  

the way the msgs are in the example it seems like they're just Boolean.  like if a msg was received msg=true. if a msg was not received then its false.  but with boolean 0 or null is false anything else is considered true. 

 

edit. you could probably use the received msg as a parameter for a function but you need to add a conditional statement like 

if $msg then

pixelsearch ect

endif 

obviously to ensure that the value is being set b4 it executes the function call.  Using the msg to pass a variable seems unnecessary bc the whole point of the function is to find that picture.  that being said you could probably use it like that to clean up your code and have less functions.  

Edited by markyrocks
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...