mhgd 发表于 2010-3-2 12:56:17

如何编写动态多线程???

本人想编写个服务器检测程序,检测主机可从配置文件中读取,所以个数是动态的,想用多线程,对检测的每台主机单独使用线程,要不然只要有一台机子ping值高,就会影响全局,但线程数量又是不固定的,要跟据主机的多少来定,如何实现,以下是多线程代码,关键就卡在最后的那个函数上,请高手指教!!!多谢!!!

$Form1 = GUICreate("多线程实例", 272, 139, -1, -1)
$Label1 = GUICtrlCreateLabel("00:00:00:00", 8, 8, 146, 17)
$Label2 = GUICtrlCreateLabel("0", 8, 56, 150, 17)
$Label3 = GUICtrlCreateLabel("", 8, 104, 148, 17)
$Button1 = GUICtrlCreateButton("关闭线程1", 168, 8, 89, 25, 0)
$Button2 = GUICtrlCreateButton("关闭线程2", 168, 48, 89, 25, 0)
$Button3 = GUICtrlCreateButton("关闭线程3", 168, 96, 89, 25, 0)
GUISetState(@SW_SHOW)
Global $t2, $t3 = 1
Dim $Timer,$TimerDLL
For $I = 0 To 2
$Timer[$I] = DllCallbackRegister("Timer", "int", "hwnd;uint;uint;dword")
$TimerDLL[$I] = DllCall("user32.dll", "uint", "SetTimer", "hwnd", 0, "uint", 0, "int", 1000, "ptr", DllCallbackGetPtr($Timer[$I]))
Next       
While 1
      Switch GUIGetMsg()
                Case - 3
                        Exit
                Case $Button1
                        DllCall("user32.dll", "int", "KillTimer", "hwnd", 0, "uint", $TimerDLL)
                        DllCallbackFree($Timer)
                Case $Button2
                        DllCall("user32.dll", "int", "KillTimer", "hwnd", 0, "uint", $TimerDLL)
                        DllCallbackFree($Timer)
                Case $Button3
                        DllCall("user32.dll", "int", "KillTimer", "hwnd", 0, "uint", $TimerDLL)
                        DllCallbackFree($Timer)
      EndSwitch
WEnd
Func Timer($hWnd, $uiMsg, $idEvent, $dwTime)
        MsgBox(4096, "Timer", "$TimerDLL:"&$TimerDLL, 10)
      If $idEvent = $TimerDLL Then
                GUICtrlSetData($Label1, @HOUR & ":" & @MIN & ":" & @SEC & ":" & @MSEC)
      ElseIf $idEvent = $TimerDLL Then
                $t2 += 1
                GUICtrlSetData($Label2, $t2)
      ElseIf $idEvent = $TimerDLL Then
                $t3 *= 2
                GUICtrlSetData($Label3, $t3)
      EndIf
EndFunc

itljl 发表于 2010-3-2 12:58:41

我以前测试过,就算写成这样的多线程,仍然是某个服务器ping高,整个GUI就卡住了。
后来用多进程模拟多线程的方法解决问题。

mhgd 发表于 2010-3-2 13:05:48

我收藏了一个他人的作品,没有用多进程,但检测速度一点都不卡,不知道他是怎么实现的,

itljl 发表于 2010-3-2 13:24:33

回复 3# mhgd


    偿试将ping的默认超时改得很小,比如1MS,这在LAN中已经够用了。
或者10MS以内。

6FINGERS 发表于 2010-3-3 11:37:26

回复mhgd


    偿试将ping的默认超时改得很小,比如1MS,这在LAN中已经够用了。
或者10MS以内。
itljl 发表于 2010-3-2 13:24 http://www.autoitx.com/images/common/back.gif


    顶楼上!

mhgd 发表于 2010-3-4 12:20:23

我做了个同时检测内网与外网的,但可以分开处理,内网设置5ms,外网设置2000ms,应该还行,

lxz 发表于 2010-3-4 13:10:19

我也想知道.....
页: [1]
查看完整版本: 如何编写动态多线程???