;;;;;;;;;;;;要在AutoIt 3.3版以后才可执行;;;;;;;;;;;;;;;;;
$Form1 = GUICreate("多线程测试", 272, 139, -1, -1) ;建立GUI视窗表单
$Label1 = GUICtrlCreateLabel("1", 8, 8, 146, 17) ;建立文字物件1
$Label2 = GUICtrlCreateLabel("1", 8, 56, 150, 17) ;建立文字物件2
$Label3 = GUICtrlCreateLabel("1", 8, 104, 148, 17) ;建立文字物件3
$OK = GUICtrlCreateButton("启动多线程", 168, 8, 89, 25, 0) ;建立启动多线程按钮
$NO = GUICtrlCreateButton("停止多线程", 168, 48, 89, 25, 0) ;建立停止多线程按钮
$S1=1 ;测试用变量可删除
$S2=1 ;测试用变量可删除
$S3=1 ;测试用变量可删除
GUISetState(@SW_SHOW) ;显示GUI视窗表单
While 1
Switch GUIGetMsg() ;侦测表单讯号(如按按钮)
Case - 3
Exit
Case $OK ;当按下启动多线程按钮时
;;;;;;;;;;三组多线程开始
$Timer1 = DllCallbackRegister("Timer", "int", "hwnd;uint;uint;dword")
$Timer1DLL = DllCall("user32.dll", "uint", "SetTimer", "hwnd", 0,"uint", 0, "int", 500,"ptr", DllCallbackGetPtr($Timer1)) ;500是该线程循环延迟的时间,可用变量来控制
$Timer2 = DllCallbackRegister("Timer", "int", "hwnd;uint;uint;dword")
$Timer2DLL = DllCall("user32.dll", "uint", "SetTimer", "hwnd", 0,"uint", 0, "int", 500, "ptr", DllCallbackGetPtr($Timer2)) ;500是该线程循环延迟的时间,可用变量来控制
$Timer3 = DllCallbackRegister("Timer", "int", "hwnd;uint;uint;dword")
$Timer3DLL = DllCall("user32.dll", "uint", "SetTimer", "hwnd",0,"uint",0, "int", 500, "ptr", DllCallbackGetPtr($Timer3)) ;500是该线程循环延迟的时间,可用变量来控制
Case $NO
;;;;;;;;;;三组多线程同时停止
DllCall("user32.dll", "int", "KillTimer", "hwnd", 0, "uint", $Timer1DLL)
DllCallbackFree($Timer1) ;该线程停止
DllCall("user32.dll", "int", "KillTimer", "hwnd", 0, "uint", $Timer2DLL)
DllCallbackFree($Timer2) ;该线程停止
DllCall("user32.dll", "int", "KillTimer", "hwnd", 0, "uint", $Timer3DLL)
DllCallbackFree($Timer3) ;该线程停止
EndSwitch
WEnd
Func Timer($hWnd, $uiMsg, $idEvent, $dwTime)
If $idEvent = $Timer1DLL[0] Then ;该线程内容(内容不能有Sleep延迟参数)
$S1=$S1+1
GUICtrlSetData($Label1, $S1)
ElseIf $idEvent = $Timer2DLL[0] Then ;该线程内容(内容不能有Sleep延迟参数)
$S2=$S2+1
GUICtrlSetData($Label2, $S2)
ElseIf $idEvent = $Timer3DLL[0] Then ;该线程内容(内容不能有Sleep延迟参数,)
$S3=$S3+1
GUICtrlSetData($Label3, $S3)
EndIf
EndFunc