找回密码
 加入
搜索
查看: 4103|回复: 6

[系统综合] 如何编写动态多线程???

  [复制链接]
发表于 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[3],$TimerDLL[3][2]
For $I = 0 To 2
$Timer[$I] = DllCallbackRegister("Timer", "int", "hwnd;uint;uint;dword")
$TimerDLL[$I][1] = 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[0])
                        DllCallbackFree($Timer[0])
                Case $Button2
                        DllCall("user32.dll", "int", "KillTimer", "hwnd", 0, "uint", $TimerDLL[1])
                        DllCallbackFree($Timer[1])
                Case $Button3
                        DllCall("user32.dll", "int", "KillTimer", "hwnd", 0, "uint", $TimerDLL[2])
                        DllCallbackFree($Timer[2])
        EndSwitch
WEnd
Func Timer($hWnd, $uiMsg, $idEvent, $dwTime)
        MsgBox(4096, "Timer", "$TimerDLL[0]:"&$TimerDLL[0][0], 10)
        If $idEvent = $TimerDLL[0][0] Then
                GUICtrlSetData($Label1, @HOUR & ":" & @MIN & ":" & @SEC & ":" & @MSEC)
        ElseIf $idEvent = $TimerDLL[1][0] Then
                $t2 += 1
                GUICtrlSetData($Label2, $t2)
        ElseIf $idEvent = $TimerDLL[2][0] Then
                $t3 *= 2
                GUICtrlSetData($Label3, $t3)
        EndIf
EndFunc
发表于 2010-3-2 12:58:41 | 显示全部楼层
我以前测试过,就算写成这样的多线程,仍然是某个服务器ping高,整个GUI就卡住了。
后来用多进程模拟多线程的方法解决问题。
 楼主| 发表于 2010-3-2 13:05:48 | 显示全部楼层
我收藏了一个他人的作品,没有用多进程,但检测速度一点都不卡,不知道他是怎么实现的,
发表于 2010-3-2 13:24:33 | 显示全部楼层
回复 3# mhgd


    偿试将ping的默认超时改得很小,比如1MS,这在LAN中已经够用了。
或者10MS以内。
发表于 2010-3-3 11:37:26 | 显示全部楼层
回复  mhgd


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



    顶楼上!
 楼主| 发表于 2010-3-4 12:20:23 | 显示全部楼层
我做了个同时检测内网与外网的,但可以分开处理,内网设置5ms,外网设置2000ms,应该还行,
发表于 2010-3-4 13:10:19 | 显示全部楼层
我也想知道.....
您需要登录后才可以回帖 登录 | 加入

本版积分规则

QQ|手机版|小黑屋|AUTOIT CN ( 鲁ICP备19019924号-1 )谷歌 百度

GMT+8, 2024-5-18 22:15 , Processed in 0.076486 second(s), 24 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表