#cs ---------------------------------------------------------------------------- AutoIt Version: 3.3.14.3 Author: KingPayc Script Function: Example of multythread based SW #ce ---------------------------------------------------------------------------- #include "AutoThreadV3.au3" #include #include #include #include #include #include #include #include ConsoleWrite("MSG EXCINAGE WORK FOLDER:"&_AuThread_Startup()) Main() func Main() $ReadMsgClokc=TimerInit() $pendingMsg="" global $MasterAliveThreadControll = ObjCreate("Scripting.Dictionary") $Form3 = GUICreate("MASTER THREAD:"&@AutoItPID, 636, 506, 1054, 315) $Button1 = GUICtrlCreateButton("NEW THREAD", 441, 8, 170, 65, $WS_GROUP) GUICtrlSetBkColor(-1, 0x00FFFF) $ThreadCtrl = GUICtrlCreateTab(16, 80, 593, 393) GUICtrlSetResizing(-1, $GUI_DOCKWIDTH+$GUI_DOCKHEIGHT) GUICtrlSetState(-1,$GUI_SHOW) GUICtrlCreateTabItem("") $WriteToAll = GUICtrlCreateEdit("", 16, 10, 300, 70) $SendToAll = GUICtrlCreateButton("WRITE TO ALL", 320, 30, 100, 25) GUICtrlSetState (-1,$GUI_DISABLE ) GUISetState(@SW_SHOW) ;$theradpid=_AuThread_StartThread("Slave",@AutoItPID) While 1 if TimerDiff($ReadMsgClokc)>500 Then ;=====READ SLAVE THREAD MESSAGES========= ;; __AuThread_RefreshAliveThreads() $pendingMsg=_AuThread_ReadNewMsg() ;;some new cmd from slave if $pendingMsg<>-1 Then local $NumberSlaveWriting=UBound($pendingMsg,1)-1 for $i=0 to $NumberSlaveWriting ;take msg data $slaveMsg = $pendingMsg[$i][1];message queue recived from $slavePid = $pendingMsg[$i][0];specific slave pid $SlaveControls = $MasterAliveThreadControll.Item($slavePid) ;get controll data where display message if IsArray($SlaveControls) then _GUICtrlTab_SetCurFocus ( $ThreadCtrl, _GUICtrlTab_FindTab($ThreadCtrl,$slavePid) ) GUICtrlSetData($SlaveControls[2],"") for $msg=0 to UBound($slaveMsg)-1 ;;write all pending msg GUICtrlSetData($SlaveControls[2],GUICtrlRead($SlaveControls[2])&$slaveMsg[$msg]) Next EndIf Next EndIf ;======================================== #Region GUI_UPDATE ;----------------------ADD TAB FOR EACH NEW SLAVE THREAD for $i=0 to UBound($_AuThrd_AliveThreadName)-1 if _GUICtrlTab_FindTab($ThreadCtrl,$_AuThrd_AliveThreadName[$i]) = -1 Then dim $SlaveCtrl[6] $SlaveCtrl[5] = GUICtrlCreateTabItem($_AuThrd_AliveThreadName[$i]) $SlaveCtrl[0] = GUICtrlCreateEdit("", 32, 128, 561, 121) $SlaveCtrl[1] = GUICtrlCreateButton("WRITE TO "&$_AuThrd_AliveThreadName[$i], 440, 256, 155, 25) $SlaveCtrl[2] = GUICtrlCreateEdit("", 32, 296, 569, 149,BitOR($WS_VSCROLL, $ES_AUTOVSCROLL, $ES_READONLY)) $SlaveCtrl[3] = GUICtrlCreateLabel("Read From "&$_AuThrd_AliveThreadName[$i], 32, 272, 86, 17) if not $MasterAliveThreadControll.Exists($_AuThrd_AliveThreadName[$i]) Then $MasterAliveThreadControll.Add($_AuThrd_AliveThreadName[$i],$SlaveCtrl) EndIf $SlaveCtrl[4]= GUICtrlCreateTabItem("") WinActivate($Form3) EndIf Next ;----------------------REMOVE TAB FOREACH CLOSED THREAD for $i=0 to _GUICtrlTab_GetItemCount ( $ThreadCtrl )-1 local $currentTaskPID1=_GUICtrlTab_GetItemText ( $ThreadCtrl,$i) if _ArraySearch($_AuThrd_AliveThreadName,$currentTaskPID1) <0 Then if $MasterAliveThreadControll.Exists($currentTaskPID1) Then $CurrentTabControls=$MasterAliveThreadControll.Item($currentTaskPID1) GUICtrlDelete($CurrentTabControls[0]) GUICtrlDelete($CurrentTabControls[1]) GUICtrlDelete($CurrentTabControls[2]) GUICtrlDelete($CurrentTabControls[3]) GUICtrlDelete($CurrentTabControls[4]) GUICtrlDelete($CurrentTabControls[5]) $MasterAliveThreadControll.Remove($currentTaskPID1) EndIf GUICtrlSetState($ThreadCtrl,$GUI_HIDE) GUICtrlSetState($ThreadCtrl,$GUI_SHOW) EndIf Next if $_AuThread_NumberSlaveAlive>0 Then GUICtrlSetState($SendToAll,$GUI_ENABLE ) Else GUICtrlSetState ($SendToAll,$GUI_DISABLE ) EndIf $ReadMsgClokc=TimerInit() #EndRegion EndIf $nMsg = GUIGetMsg() Switch $nMsg Case $GUI_EVENT_CLOSE Exit Case $Button1 ;;START NEW THREAD Sleep(100+random(50));more col diff $theradpid=_AuThread_StartThread("Slave",@AutoItPID) ;START NEW THREAD PID Case $SendToAll ;;SEND MSG TO ALL SUBTHREAD local $txtTosend=GUICtrlRead($WriteToAll) _AuThread_SendMsg($txtTosend) GUICtrlSetData($WriteToAll,"") CASE Else ;PID TAB ARRAY CONTROLL MANAGEMENT (GET BUTTONS FOR SEND MSG TO SPECIFIC THREAD) $aKeys = $MasterAliveThreadControll.Keys For $i = 0 To $MasterAliveThreadControll.Count -1 local $currentTaskPID=$aKeys[$i] local $CurrentTabControls=$MasterAliveThreadControll.Item($currentTaskPID) if IsArray($CurrentTabControls) then if $nMsg = $CurrentTabControls[1] Then local $txtTosend=GUICtrlRead( $CurrentTabControls[0] ) If StringLen($txtTosend)>0 then _AuThread_SendMsg($txtTosend,$currentTaskPID) GUICtrlSetData($CurrentTabControls[0],"") EndIf EndIf EndIf Next EndSwitch WEnd EndFunc ;;BEGIN SLAVE CODE, THERE'S NO LIMIT @ WHAT CAN BE DONE WITH SLAVE THREAD func Slave () Sleep(random(@msec)) $ReadMsgClokc=TimerInit() $pendingMsg="" #Region ### START Koda GUI section ### Form= $Form2 = GUICreate("Slave Thread PID:"&@AutoItPID, 400, 425, Random(100,@DesktopWidth-500), Random(100,@Desktopheight-500)) Sleep(@MSEC) GUISetBkColor("0x" & Hex(Random(1, 255, 1) & Random(1, 255, 1) & Random(1, 255, 1), 6)) $Edit1 = GUICtrlCreateEdit("", 24, 16, 361, 89) $SEND = GUICtrlCreateButton("SEND MSG TO MASTER", 208, 112, 163, 25, $WS_GROUP) $List1 = GUICtrlCreateEdit("", 32, 176, 345, 240,BitOR($WS_VSCROLL, $ES_AUTOVSCROLL, $ES_READONLY)) $Label1 = GUICtrlCreateLabel("MSG FROM MASTER PID:"&$_AuThread_Master_Pid, 32, 152, 1500, 17) GUISetState(@SW_SHOW) #EndRegion ### END Koda GUI section ### While 1 ;TIMER TICK EACH 50MS FOR READ NEW MASTER MSG if TimerDiff($ReadMsgClokc)>500 Then $pendingMsg=_AuThread_ReadNewMsg() if UBound($pendingMsg)>0 then GUICtrlSetData ($List1,"") for $i=0 to UBound($pendingMsg)-1 GUICtrlSetData ($List1,GUICtrlRead($List1)&$pendingMsg[$i]) WinActivate($Form2) Next EndIf ;;Test send always multiple messages ;_AuThread_SendMsg(@AutoItPID&" "&@SEC&"."&@MSEC&@cr) $ReadMsgClokc=TimerInit() EndIf $nMsg = GUIGetMsg() Switch $nMsg Case $GUI_EVENT_CLOSE Exit Case $SEND ;;SEND MSG TO MASTER _AuThread_SendMsg(GUICtrlRead($Edit1)) GUICtrlSetData($Edit1,"") EndSwitch WEnd EndFunc