Jump to content

Autoit multi thread


ZeroClock
 Share

Recommended Posts

#include <Array.au3>

Opt("MustDeclareVars", 1)

Func Test_1($x)
    Local $Array = _STCBF_Struct($x)
    Local $At = ''
    For $i = 0 To (UBound($Array)-1) Step 1
        $At &= $Array[$i] &' - '
    ;ConsoleWrite("$Array["& $i &"] = "& $Array[$i] &@CRLF )
    Next
    MsgBox(0x40, "Thread-1", "Added Thread #1" &@CRLF&@CRLF& StringLeft($At,(StringLen($At) -3)) )
EndFunc ;==> _Thread_Start

Func Test_2($x)
    Local $Array = _STCBF_Struct($x)
    Local $At = ''
    For $i = 0 To (UBound($Array)-1) Step 1
        $At &= $Array[$i] &' - '
    ;ConsoleWrite("$Array["& $i &"] = "& $Array[$i] &@CRLF )
    Next
    MsgBox(0x40, "Thread-2", "Added Thread #2" &@CRLF&@CRLF& StringLeft($At,(StringLen($At) -3)) )
EndFunc ;==> _Thread_Start

Func Test_3($x)
    Local $Array = _STCBF_Struct($x)
    Local $At = ''
    For $i = 0 To (UBound($Array)-1) Step 1
        $At &= $Array[$i] &' - '
    ;ConsoleWrite("$Array["& $i &"] = "& $Array[$i] &@CRLF )
    Next
    MsgBox(0x40, "Thread-3", "Added Thread #3" &@CRLF&@CRLF& StringLeft($At,(StringLen($At) -3)) )
EndFunc ;==> _Thread_Start

Local $Ax[4] = [100, 'A', 200, 'B']
Local $N = 10
Local $Bx[4] = [300, 'C', 400, 'D']

    _Sub_Thread_CallBack_Func("Test_1", $Ax)
    _Sub_Thread_CallBack_Func("Test_2", $N)
    _Sub_Thread_CallBack_Func("Test_3", $Bx)

MsgBox(0x40, "Thread-0", "## Default_Thread ##")

;#################################################################################################################################################
Func _Sub_Thread_CallBack_Func($F, ByRef $P)
    Local $Px = "", $L = StringLen($P)
    If IsArray($P) Then
        For $i = 0 To (UBound($P)-1) Step 1
            $Px &= $P[$i] &","
        Next
        $Px = StringLeft($Px, (StringLen($Px) -1) )
        $L = StringLen($Px)
        $P = $Px
    EndIf
    Local $H = DllCallbackRegister($F, "int", "DWORD_PTR")
    Local $S = DllStructCreate("INT; Char["& $L &"]")
    DllStructSetData($S, 2, $P)
    DllStructSetData($S, 1, $L)
    Local $R = DllCall("kernel32.dll", "hwnd", "CreateThread", "ptr", 0, "dword", 0, _
                       "long", DllCallbackGetPtr($H), "ptr", DllStructGetPtr($S), "long", 0, "int*", 0)
    ;DllCallbackFree($H)
    ;Return $R
    Sleep(10)
EndFunc
Func _STCBF_Struct(ByRef $x)
    Local $y = DllStructGetData(DllStructCreate("INT; Char["& DllStructGetData(DllStructCreate("INT; Char[1]", $x), 1) &"]", $x), 2)
    Local $Ar = StringSplit($y, ',', 2)
    Return $Ar
EndFunc
;#################################################################################################################################################

creates a thread and already passes the data in a array.

Link to comment
Share on other sites

  • Moderators

ZeroClock

As the AutoIt developers have always stated that AutoIt is not thread safe, I would like to point out that readers use this code at their own risk and should not expect any official support.

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

  • Moderators

ZeroClock,

Quote

you could apply a traffic light ? it would be possible ?

What do you mean?

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

7 minutes ago, ZeroClock said:

Melba23

you could apply a traffic light ? it would be possible ?

 

5 minutes ago, Melba23 said:

ZeroClock,

What do you mean?

M23

if you use a mutex approach , become thread-safe ?

Link to comment
Share on other sites

  • Moderators

ZeroClock,

All I know is what I posted above. The Devs have always said that AutoIt is not thread safe so I doubt there is any easy fix or they would have introduced it given the number of times this subject comes up.

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

16 minutes ago, Melba23 said:

ZeroClock,

Tutto quello che so è quello che ho postato sopra. Gli sviluppatori hanno sempre detto che AutoIt non è thread-safe quindi dubito ci sia alcuna soluzione facile o che avrebbe introdotto è dato il numero di volte in cui questo argomento viene in su.

M23

OK thanks

Link to comment
Share on other sites

#include <Array.au3>
#include <GUIConstantsEx.au3>

Opt("MustDeclareVars", 1)

Local $Ax[4] = [100, 'A', 200, 'B']
Local $N = 10
Local $Bx[4] = [300, 'C', 400, 'D']

;~ MsgBox(0x40, "Thread-0", "## Default_Thread ##")

; Create a GUI with various controls.
Local $hGUI = GUICreate("Example")
Local $btnOK = GUICtrlCreateButton("OK", 310, 370, 85, 25)

; Display the GUI.
GUISetState(@SW_SHOW, $hGUI)


_Sub_Thread_CallBack_Func("Test_1", $Ax)
_Sub_Thread_CallBack_Func("Test_2", $N)
_Sub_Thread_CallBack_Func("Test_3", $Bx)

; Loop until the user exits.
While 1
        Switch GUIGetMsg()
            Case $GUI_EVENT_CLOSE, $btnOK
                ExitLoop

        EndSwitch
WEnd

; Delete the previous GUI and all controls.
GUIDelete($hGUI)


Func Test_1($x)
    Local $Array = _STCBF_Struct($x)
    Local $At = ''
    For $i = 0 To (UBound($Array)-1) Step 1
        $At &= $Array[$i] &' - '
    ;ConsoleWrite("$Array["& $i &"] = "& $Array[$i] &@CRLF )
    Next
    MsgBox(0x40, "Thread-1", "Added Thread #1" &@CRLF&@CRLF& StringLeft($At,(StringLen($At) -3)) )
EndFunc ;==> _Thread_Start

Func Test_2($x)
    Local $Array = _STCBF_Struct($x)
    Local $At = ''
    For $i = 0 To (UBound($Array)-1) Step 1
        $At &= $Array[$i] &' - '
    ;ConsoleWrite("$Array["& $i &"] = "& $Array[$i] &@CRLF )
    Next
    MsgBox(0x40, "Thread-2", "Added Thread #2" &@CRLF&@CRLF& StringLeft($At,(StringLen($At) -3)) )
EndFunc ;==> _Thread_Start

Func Test_3($x)
    Local $Array = _STCBF_Struct($x)
    Local $At = ''
    For $i = 0 To (UBound($Array)-1) Step 1
        $At &= $Array[$i] &' - '
    ;ConsoleWrite("$Array["& $i &"] = "& $Array[$i] &@CRLF )
    Next
    MsgBox(0x40, "Thread-3", "Added Thread #3" &@CRLF&@CRLF& StringLeft($At,(StringLen($At) -3)) )
EndFunc ;==> _Thread_Start



;#################################################################################################################################################
Func _Sub_Thread_CallBack_Func($F, ByRef $P)
    Local $Px = "", $L = StringLen($P)
    If IsArray($P) Then
        For $i = 0 To (UBound($P)-1) Step 1
            $Px &= $P[$i] &","
        Next
        $Px = StringLeft($Px, (StringLen($Px) -1) )
        $L = StringLen($Px)
        $P = $Px
    EndIf
    Local $H = DllCallbackRegister($F, "int", "DWORD_PTR")
    Local $S = DllStructCreate("INT; Char["& $L &"]")
    DllStructSetData($S, 2, $P)
    DllStructSetData($S, 1, $L)
;~     Local $R = DllCall("kernel32.dll", "hwnd", "CreateThread", "ptr", 0, "dword", 0, _
;~                        "long", DllCallbackGetPtr($H), "ptr", DllStructGetPtr($S), "long", 0, "int*", 0)
    Local $R = DllCall("kernel32.dll", "hwnd", "CreateThread", "ptr", 0, "dword", 0, _
                       "LONG_PTR", DllCallbackGetPtr($H), "LONG_PTR", DllStructGetPtr($S), "long", 0, "int*", 0)

    ;DllCallbackFree($H)
    ;Return $R
    Sleep(10)
EndFunc
Func _STCBF_Struct(ByRef $x)
    Local $y = DllStructGetData(DllStructCreate("INT; Char["& DllStructGetData(DllStructCreate("INT; Char[1]", $x), 1) &"]", $x), 2)
    Local $Ar = StringSplit($y, ',', 2)
    Return $Ar
EndFunc
;#################################################################################################################################################

Above W10, 64 bits but directly shows problems when GUI is "stolen" from mainthread

Link to comment
Share on other sites

Quote

multiple threads on this subject Multithreading

Or

"While the forum is multithreaded, AutoIt is not.  Unsafe IPC is the best you will ever get, ever."

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

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