Jump to content

Recommended Posts

Posted

Array / object / map > not supported.

ExampleA:

#include "ProcessManager.au3"

__AutoItThread_Bootstrapper()
__ExampleA()

Func __ExampleA()
    Local $aTest1Params[2] = ["Test 1 Title", "This is the message payload!"]

    Local $hProc1 = __AutoItCreateThread("Test1", $aTest1Params)
    Local $hProc2 = __AutoItCreateThread("Test2")
    Local $hProc3 = __AutoItCreateThread("Test3", "5")

    MsgBox(0, "Main Process", "All 3 background processes have been fired off concurrently!")

    Local $sResult1 = __AutoItWaitThread($hProc1)
    Local $sResult2 = __AutoItWaitThread($hProc2)
    Local $sResult3 = __AutoItWaitThread($hProc3)

    MsgBox(0, "Main Process - Received Data", "Test1 returned:>  " & $sResult1)
    MsgBox(0, "Main Process", "All threads completed successfully.")
EndFunc

Func Test1($sTitle, $sMessage)
    Local $iResp = MsgBox(0, $sTitle, $sMessage)
    Return "Done with value: " & $iResp
EndFunc

Func Test2()
    MsgBox(0, "2", "Test 2 Executed")
EndFunc

Func Test3($iTimeout)
    MsgBox(0, "3", "Test 3 checking in with timeout: " & $iTimeout, Number($iTimeout))
EndFunc

 

ExampleB:

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <WinAPIFiles.au3>
#include <WinAPIHObj.au3>
#include "ProcessManager.au3"

Global Const $sStructFormat = "int NewMsg;int ExitFlag;char Msg[512];"
Global Const $iStructSize = DllStructGetSize(DllStructCreate($sStructFormat))
Global $g_idLabel

__AutoItThread_Bootstrapper()
__ExampleB()

Func __ExampleB()
    Local $sUniqueMapName = "Local\AutoItThreadMap_" & @AutoItPID & "_" & int(TimerInit())

    Local $hSharedMapping = _WinAPI_CreateFileMapping(-1, $iStructSize, $sUniqueMapName)
    If @error Or Not $hSharedMapping Then Exit MsgBox(16, "Error", "Parent failed to create file mapping.")

    Local $pSharedAddress = _WinAPI_MapViewOfFile($hSharedMapping)
    If @error Or Not $pSharedAddress Then
        _WinAPI_CloseHandle($hSharedMapping)
        Exit MsgBox(16, "Error", "Parent failed to map view of file.")
    EndIf
    Local $tStruct2 = DllStructCreate($sStructFormat, $pSharedAddress)

    Local $hGUI = GUICreate("To-and-Forth Controller", 400, 200)
    Local $idInput = GUICtrlCreateInput("Hello Worker!", 20, 30, 360, 20)
    Local $idSend  = GUICtrlCreateButton("Send Message to Worker", 20, 70, 180, 30)
    Local $idExit  = GUICtrlCreateButton("Shutdown Worker & Exit", 210, 70, 170, 30)
    $g_idLabel = GUICtrlCreateLabel("Last Reply: None", 20, 130, 360, 40)

    GUIRegisterMsg($WM_COPYDATA, "__OnThreadReply")
    GUISetState(@SW_SHOW, $hGUI)

    Local $aWorkerArgs[2] = [String($hGUI), $sUniqueMapName]
    Local $hProc2 = __AutoItCreateThread("__Background_Message_Thread", $aWorkerArgs)
    If @error Then Exit MsgBox(16, "Error", "Failed to create background process")

    Local $sResponse = ''
    While True
        Switch GUIGetMsg()
            Case $GUI_EVENT_CLOSE, $idExit
                DllStructSetData($tStruct2, "ExitFlag", 1)
                $sResponse = __AutoItWaitThread($hProc2)
                ExitLoop

            Case $idSend
                Local $sTextToSend = GUICtrlRead($idInput)
                If $sTextToSend == "" Then ContinueLoop

                DllStructSetData($tStruct2, "Msg", $sTextToSend)
                DllStructSetData($tStruct2, "NewMsg", 1)
                GUICtrlSetData($g_idLabel, "Status: Message dispatched, awaiting WM_COPYDATA response...")
        EndSwitch

        Sleep(10)
    WEnd

    _WinAPI_UnmapViewOfFile($pSharedAddress)
    _WinAPI_CloseHandle($hSharedMapping)
    MsgBox(64, "Parent Process", "Worker thread exited. Return pipe string value: " & $sResponse)
EndFunc


Func __OnThreadReply($hWnd, $iMsg, $wParam, $lParam)
    Local $tCopyData = DllStructCreate("ulong_ptr dwData;dword cbData;ptr lpData", $lParam)
    Local $iBytes     = DllStructGetData($tCopyData, "cbData")
    Local $pData      = DllStructGetData($tCopyData, "lpData")

    Local $tStringBuf = DllStructCreate("wchar[" & ($iBytes / 2) & "]", $pData)
    Local $sReceivedMessage = DllStructGetData($tStringBuf, 1)

    GUICtrlSetData($g_idLabel, "Last Worker Reply: " & $sReceivedMessage)
    Return 1
EndFunc

Func __Background_Message_Thread($hNotifyWindow, $sMapName)
    Local $hWndTarget = HWnd($hNotifyWindow)

    Local $hWorkerMapping = _WinAPI_OpenFileMapping($sMapName, 0x0002)
    If @error Or Not $hWorkerMapping Then Return "Error: Worker failed to open file mapping layout."

    Local $pWorkerAddress = _WinAPI_MapViewOfFile($hWorkerMapping)
    If @error Or Not $pWorkerAddress Then
        _WinAPI_CloseHandle($hWorkerMapping)
        Return "Error: Worker failed to view memory layout mapping."
    EndIf

    Local $tLocalStruct = DllStructCreate($sStructFormat, $pWorkerAddress)
    While True
        If DllStructGetData($tLocalStruct, "ExitFlag") = 1 Then
            ExitLoop
        EndIf

        If DllStructGetData($tLocalStruct, "NewMsg") = 1 Then
            Local $sSharedText = DllStructGetData($tLocalStruct, "Msg")
            Local $sProcessedReply = "Processed: [" & $sSharedText & "] at " & @SEC & "s"

            DllStructSetData($tLocalStruct, "NewMsg", 0)
            __WM_COPYDATA_SendData($hWndTarget, $sProcessedReply)
        EndIf
        Sleep(50)
    WEnd

    _WinAPI_UnmapViewOfFile($pWorkerAddress)
    _WinAPI_CloseHandle($hWorkerMapping)

    Return '-Done ok-'
EndFunc

 

ProcessManager.au3

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
×
×
  • Create New...