Jump to content

UDF : Multi-threading made easy


Go to solution Solved by Nine,

Recommended Posts

  • 3 months later...
Posted (edited)

@Ghost_Line If the array is not too big, you could pass a string (use _ArrayToString to create it).  Using WCD-IPC might be a little unnecessary complex.

Here a simple example :

#include "..\Files\PMT-UDF.au3"
#include <Array.au3>

Local $aArray = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]

_PMT_Init()

$sString = _ArrayToString($aArray)

_PMT_Start("DisplayArray", $sString)

While Sleep(100)
WEnd

Func DisplayArray($sStr)
  Local $aArr = _ArrayFromString($sStr)
  _ArrayDisplay($aArr)
EndFunc

 

Edited by Nine
  • Solution
Posted (edited)

@Ghost_Line  No worries.  Made me think what would be a good way to communicate large amount of data between 2 threads and I came up with this solution.

#include "..\Files\PMT-UDF.au3"
#include <Array.au3>
#include <WinAPIFiles.au3>
#include <WinAPIHObj.au3>

Local $aLargeArray[500][20]
For $i = 0 To UBound($aLargeArray, 1) - 1
  For $j = 0 To UBound($aLargeArray, 2) - 1
    $aLargeArray[$i][$j] = $i & "-" & $j
  Next
Next

_PMT_Init()

Local $hMapping = _WinAPI_CreateFileMapping(-1, 65000, 'FileMapping')
If Not $hMapping Or @extended Then Exit MsgBox($MB_OK, "Error", "Unable to create file mapping @extended = " & @extended)
Local $pAddress = _WinAPI_MapViewOfFile($hMapping)
Local $tData = DllStructCreate('char content[65000]', $pAddress)
$tData.content = _ArrayToString($aLargeArray, Default, Default, Default, @CR)

Local $hProc = _PMT_Start("Receiver", "FileMapping", 65000)

Local $sResponse
While Sleep(50)
  $sResponse = _PMT_GetResponse($hProc)
  If @error Then Exit MsgBox($MB_OK, "Error", "Process has dropped")
  If $sResponse <> "" Then
    MsgBox($MB_OK + $MB_TOPMOST, "Response", $sResponse & @CRLF)
    ExitLoop
  EndIf
WEnd

_WinAPI_UnmapViewOfFile($pAddress)
_WinAPI_CloseHandle($hMapping)

Func Receiver($sName, $iSize)
  Local $hMapping = _WinAPI_OpenFileMapping($sName)
  If Not $hMapping Then Return 0

  Local $pAddress = _WinAPI_MapViewOfFile($hMapping)
  Local $tData = DllStructCreate("char content[" & $iSize & "]", $pAddress)
  Local $sText = $tData.content
  Local $aLargeArray = _ArrayFromString($sText, Default, @CR)
  _ArrayDisplay($aLargeArray)

  _WinAPI_UnmapViewOfFile($pAddress)
  _WinAPI_CloseHandle($hMapping)
  Return 1
EndFunc   ;==>_Receiver

 

Edited by Nine

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
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...