Jump to content

Here is an Idea for pretend multi threading


ChrisL
 Share

Recommended Posts

OK whats your opinion on this, a few people have asked about multi threading and sugestions are usually run 2 exe's and a text file to communicate

What about having one exe which writes a new AU3 file and then runs it..

and the way you talk to the main application and return a value is by using TCP/IP to send/receive a message. The adlib check for a new message periodically.

I've just tried it with creating a simple second app that just sends a line of text back but I think it could be used for greater things.

; ----------------------------------------------------------------------------
;
; AutoIt Version: 3.1.103 beta
; Author:        Chris Lambert
;
; Script Function:
;   Template AutoIt script.
;
; ----------------------------------------------------------------------------

; Script Start - Add your code below here

AdlibEnable ( "Adlib" , 1000 )
$Port = 8001
Global $MaxConc = 100
$IPAddress = @IPAddress1
Global $MainSocket = TCPStartServer($Port, $MaxConc)
If @error <> 0 Then Exit MsgBox(16, "Error", "Server unable to initialize." & @crlf & _
        "Check the correct network card is set and port is not already in use" & @crlf & _
        "Selected network card address is :" & $IPAddress & " Port number is :" & $Port)
Global Const $MaxLength = 512
Global $ConnectedSocket[$MaxConc]
Global $CurrentSocket = 0
Local $Track = 0
Global Const $MaxConnection = ($MaxConc - 1)

For $Track = 0 To $MaxConnection Step 1
    $ConnectedSocket[$Track] = -1
Next

;; This section writes the new file and then runs it
$NewTempFile = @Hour & @Min & @sec & ".Au3"
FileWriteLine ($NewTempFile,'MsgBox (0,@scriptname,"I am the new file running and the main program is still doing its own thing")')
FileWriteLine ($NewTempFile, 'Global $windowTitle')
FileWriteLine ($NewTempFile, '$Port = 8001')
FileWriteLine ($NewTempFile, 'Global $MainSocket')
FileWriteLine ($NewTempFile,';perform some actions here then send the data back to the main application')
FileWriteLine ($NewTempFile,'       SendData("Info I want to send back to main application");Send data back')


FileWriteLine ($NewTempFile,'Func SendData($Data)')
FileWriteLine ($NewTempFile,'   TCPStartup()')
FileWriteLine ($NewTempFile,'   $MainSocket = TCPConnect(TCPNameToIP(@computername), $Port)')
FileWriteLine ($NewTempFile,'   If $MainSocket = -1 Then;send message success first attempt')

FileWriteLine ($NewTempFile,'       While $MainSocket = -1')
FileWriteLine ($NewTempFile,'           $MainSocket = TCPConnect(TCPNameToIP(@computername), $Port)')
FileWriteLine ($NewTempFile,'           If $MainSocket <> - 1 Then ExitLoop;exit for loop')
FileWriteLine ($NewTempFile,'               Sleep (1000)')
FileWriteLine ($NewTempFile,'           Wend')
FileWriteLine ($NewTempFile,'   Endif')
FileWriteLine ($NewTempFile,'           TCPSend($MainSocket, $Data)')
FileWriteLine ($NewTempFile,'EndFunc ;==>SendData')

FileWriteLine ($NewTempFile,'Func OnAutoItExit()')
FileWriteLine ($NewTempFile,'   TCPCloseSocket($MainSocket)')
FileWriteLine ($NewTempFile,'   TCPShutdown()')
FileWriteLine ($NewTempFile,'EndFunc ;==>OnAutoItExit')
;;end of new file generation

Run(@AutoItExe & ' /AutoIt3ExecuteScript ' & $NewTempFile)




    For $i = 1 to 10
    MsgBox (0,"Main", "I am the main program",1)
    Next



Func Adlib()
            $ConnectedSocket[$CurrentSocket] = TCPAccept($MainSocket)
            If $ConnectedSocket[$CurrentSocket] <> - 1 Then
                $CurrentSocket = SocketSearch()
            EndIf
            $Track = 0
            For $Track = 0 To $MaxConnection Step 1
                If $ConnectedSocket[$Track] <> - 1 Then
                    $Data = TCPRecv($ConnectedSocket[$Track], $MaxLength)
                    If StringRight($Data, 4) = "~bye" Then
                        TCPCloseSocket($ConnectedSocket[$Track])
                        $ConnectedSocket[$Track] = -1
                        $CurrentSocket = SocketSearch()
                    ElseIf $Data <> "" Then
                        ReceivedData($Data)
                    EndIf
                EndIf
            Next
EndFunc


Func ReceivedData($Cmd)
MsgBox (0,"My received Data", $CMD)
EndFunc ;==>ReceivedData


Func SocketSearch()
    Local $Track = 0
    For $Track = 0 To $MaxConnection Step 1
        If $ConnectedSocket[$Track] = -1 Then
            Return $Track
        Else
    ; Socket In Use
        EndIf
    Next
EndFunc ;==>SocketSearch

Func TCPStartServer($Port, $MaxConnect = 1)
    Local $Socket
    $Socket = TCPStartup()
    Select
        Case $Socket = 0
            SetError(@error)
            Return -1
    EndSelect
    $Socket = TCPListen($IPAddress, $Port, $MaxConnect)
    Select
        Case $Socket = -1
            SetError(@error)
            Return 0
    EndSelect
    SetError(0)
    Return $Socket
EndFunc ;==>TCPStartServer
Edited by ChrisL
Link to comment
Share on other sites

moin, moin...:o!

It´s a real good idea and maybe that´s a practicably way, but that will run only successful, if you have AutoIt installed on the computer where it should run. I think it should be a separately exe file with predefines functions like a progress bar or similar. This exe will be called from the programm, with commandline parameters it could become a little flexible.

Greets

Rudika

Edited by rudika

[font="Comic Sans Ms"][center]Powered by AutoIt3http://www.wik-eric.de/zips/Synchro2.2.2-4free.zip[/center][/font]

Link to comment
Share on other sites

moin, moin...:o!

It´s a real good idea and maybe that´s a practicably way, but that will run only successful, if you have AutoIt installed on the computer where it should run. I think it should be a separately exe file with predefines functions like a progress bar or similar. This exe will be called from the programm, with commandline parameters it could become a little flexible.

Greets

Rudika

It should run without AutoIT installed because AutoIT will be compiled in the main application

Link to comment
Share on other sites

So you'll have 2 running exe as well (main.exe and autoit.exe).

Your main app will run, then generate a new script and call the new script with AutoITexec, it can send data back via the TCP connection instead of using a text file. Well that's the plan anyway :o

Link to comment
Share on other sites

  • Moderators

Your main app will run, then generate a new script and call the new script with AutoITexec, it can send data back via the TCP connection instead of using a text file. Well that's the plan anyway :o

I personally like the idea!!

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.

Link to comment
Share on other sites

You don't need to use TCP connections, you can use the STD handles to communicate. See the Console* functions and Std* functions in the beta help file.

The gist of what you do is Run() the child script with the replace std handle options set (STDIN, STDOUT). The parent script can send data to the child over the child's STDIN stream. The parent script uses StdinWrite() to send the data and the child uses ConsoleRead() to read it. The child can use ConsoleWrite() to send data to the STDOUT stream which can be read from the parent with StdoutRead().

Link to comment
Share on other sites

  • Moderators

You don't need to use TCP connections, you can use the STD handles to communicate. See the Console* functions and Std* functions in the beta help file.

The gist of what you do is Run() the child script with the replace std handle options set (STDIN, STDOUT). The parent script can send data to the child over the child's STDIN stream. The parent script uses StdinWrite() to send the data and the child uses ConsoleRead() to read it. The child can use ConsoleWrite() to send data to the STDOUT stream which can be read from the parent with StdoutRead().

Any chance of getting a working example script... I would truly like to understand it from a working example.

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.

Link to comment
Share on other sites

Just a slight comment if you don't use the communication as mention by Valik or ChrisL.

If main script is compiled you don't need to have AutoIt install just run (@AutoItExe & " /AutoIt3ExecuteScript yourgeneratedscriptfile") it will reuse the AutoIt executable which allow your main script to run. :o

Link to comment
Share on other sites

Any chance of getting a working example script... I would truly like to understand it from a working example.

Maybe this is what is meant.. maybe there is a slicker way of doing it too?

; ----------------------------------------------------------------------------
;
; AutoIt Version: 3.1.103 beta
; Author:        Chris Lambert
;
; Script Function:
;   Template AutoIt script.
;
; ----------------------------------------------------------------------------

; Script Start - Add your code below here

AdlibEnable("Adlib", 1000)
Dim $Foo[100]; maximum number of child processes

$NewTempFile = Int (Random () * 10000)  & ".Au3"
FileWriteLine ($NewTempFile,'MsgBox (0,@scriptname,"I am the new file 1 running and the main program is still doing its own thing", 2)')
FileWriteLine ($NewTempFile,'ConsoleWrite ( "Info I want to send back to main application from child Number 1") ')
AddSuicide(); add self delete to end of new file
$Foo[1] = Run(@AutoItExe & ' /AutoIt3ExecuteScript ' & $NewTempFile,@scriptdir,  @SW_show, 7)


$NewTempFile = Int (Random () * 10000)  & ".Au3"
FileWriteLine ($NewTempFile,'MsgBox (0,@scriptname,"I am the new file 2 running and the main program is still doing its own thing", 2)')
FileWriteLine ($NewTempFile,'ConsoleWrite ( "Info I want to send back to main application from child Number 2") ')
AddSuicide(); add self delete to end of new file
$Foo[2] = Run(@AutoItExe & ' /AutoIt3ExecuteScript ' & $NewTempFile,@scriptdir,  @SW_show, 7)

    For $i = 1 to 5
    MsgBox (0,"Main", "I am the main program",2)
    Next


Func Adlib ()
    For $i = 1 to Ubound ($Foo) - 1 
        If @error then exitloop
    $line = StdoutRead($foo[$i],"",true)
    If $Line <> 0 then 
        $line = StdoutRead($foo[$i])
    MsgBox(0, "STDOUT read:", $line)
EndIf
Next
EndFunc

Func AddSuicide();; Adds the self delete batch to the temp script
FileWriteLine ($NewTempFile , 'Func OnAutoItExit()')
FileWriteLine ($NewTempFile , '$SC_File = @TEMPDIR & "\suicide.bat"')
FileWriteLine ($NewTempFile ,'  FileDelete($SC_File)')
FileWriteLine ($NewTempFile ,'  $SC_batch = "loop:" & @CRLF & "del " & """" & @SCRIPTFULLPATH & """"  & @CRLF & _')
FileWriteLine ($NewTempFile ,'       "ping -n 1 -w 250 zxywqxz_q" & @CRLF & "if exist " & @SCRIPTFULLPATH & _')
FileWriteLine ($NewTempFile ,'      " goto loop" & @CRLF & "del suicide.bat" & @CRLF')

FileWriteLine ($NewTempFile ,'  FileWrite($SC_File,$SC_batch)')
FileWriteLine ($NewTempFile ,'  Run($SC_File,@TEMPDIR,@SW_HIDE) ')
FileWriteLine ($NewTempFile ,'  Exit    ')
FileWriteLine ($NewTempFile ,'EndFunc')
EndFunc
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...