我扩展了一下1楼的第二段代码,用主线程传给回调函数参数的形式实现了各线程进行不同的简单的运算,但即使是这样的简单运算,程序有时也会出错~
代码:
$Handle1 = DllCallbackRegister("ThreadTest1", "int", "ptr")
$Handle2 = DllCallbackRegister("ThreadTest2", "int", "ptr")
Func CreateThread($Handle, $struct)
$return = DllCall("kernel32.dll", "hwnd", "CreateThread", "ptr", 0, "dword", 0, "long", DllCallbackGetPtr($Handle), "ptr", DllStructGetPtr($struct), "long", 0, "int*", 0)
Return $return[0]
EndFunc
$Struct1 = DllStructCreate("Char[200];int")
DllStructSetData($Struct1, 1, "23|45")
CreateThread($Handle1, $Struct1)
$Struct2 = DllStructCreate("Char[200];int")
DllStructSetData($Struct2, 1, '10|5')
CreateThread($Handle2, $Struct2)
$result = 25 ^3
MsgBox(0, "线程1", "默认线程"& @CRLF & _
'25^3='&$result)
Func ThreadTest1($x)
$y = DllStructCreate("char[200];int", $x)
$su = DllStructGetData ($y,1)
$su2 = StringSplit($su,"|")
$result = $su2[1]*$su2[2]
MsgBox(0,"线程2", "两数相乘"&@CRLF& _
$su2[1]&"*"&$su2[2]&"="&$result)
EndFunc ;==>_ThreadStart
Func ThreadTest2($x)
$y = DllStructCreate("char[200];int", $x)
$su = DllStructGetData ($y,1)
$su2 = StringSplit($su,"|")
$result = $su2[1]+$su2[2]
MsgBox(0,"线程3", "两数相加"&@CRLF& _
$su2[1]&"+"&$su2[2]&"="&$result)
EndFunc ;==>_ThreadStart
|