找回密码
 加入
搜索
查看: 4349|回复: 8

[系统综合] 怎样运行两个函数互相不干扰,请给出代码[已解决]

  [复制链接]
发表于 2010-12-17 13:08:54 | 显示全部楼层 |阅读模式
本帖最后由 leon460 于 2011-7-11 22:30 编辑

有时候写一个小程序时,里面有函数调用会相互影响,甚是困扰,比如下图,请各位给出方法,(写出代码哇,我是菜鸟),看下面代码

谢谢各位
$i=1
Do        
        _main1()
        _main2()
Until $i=0
func _main1()
        Sleep(1000)
        ;语句
EndFunc

func _main2()
        Sleep(99999999)
        ;语句
EndFunc
发表于 2010-12-17 13:34:08 | 显示全部楼层
经测试,楼主代码挺好的,未发现有干扰情况出现,更别说困扰
发表于 2010-12-17 14:16:15 | 显示全部楼层
是想两个函数同时运行吧??这样要搞多线程。。
贴个多线程的实例给你,再是否适用
$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
$Timer = DllCallbackRegister("Timer", "int", "hwnd;uint;uint;dword")
$TimerDLL = DllCall("user32.dll", "uint", "SetTimer", "hwnd", 0, "uint", 0, "int", 1000, "ptr", DllCallbackGetPtr($Timer))
$Timer2 = DllCallbackRegister("Timer", "int", "hwnd;uint;uint;dword")
$Timer2DLL = DllCall("user32.dll", "uint", "SetTimer", "hwnd", 0, "uint", 0, "int", 200, "ptr", DllCallbackGetPtr($Timer2))
$Timer3 = DllCallbackRegister("Timer", "int", "hwnd;uint;uint;dword")
$Timer3DLL = DllCall("user32.dll", "uint", "SetTimer", "hwnd", 0, "uint", 0, "int", 500, "ptr", DllCallbackGetPtr($Timer3))
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", $Timer2DLL)
;~             DllCallbackFree($Timer2)
        Case $Button3
            DllCall("user32.dll", "int", "KillTimer", "hwnd", 0, "uint", $Timer3DLL)
            DllCallbackFree($Timer3)
    EndSwitch
WEnd
Func Timer($hWnd, $uiMsg, $idEvent, $dwTime)
    If $idEvent = $TimerDLL[0] Then
        GUICtrlSetData($Label1, @HOUR & ":" & @MIN & ":" & @SEC & ":" & @MSEC)
    ElseIf $idEvent = $Timer2DLL[0] Then
        $t2 += 1
        GUICtrlSetData($Label2, $t2)
    ElseIf $idEvent = $Timer3DLL[0] Then
        $t3 *= 2
        GUICtrlSetData($Label3, $t3)
    EndIf
EndFunc
发表于 2010-12-17 17:28:13 | 显示全部楼层
回复 1# leon460


相互干扰 是 一定会的
如果说干扰是运行了某一函数,由于函数内某一代码的原因,致使其挂起而影响到其他函数的执行!
首先要找的是是运行了什么代码,这个挂起的代码是不是需要的
!
如果要同时运行多个函数,你可以引入 AdlibRegister() 或 3 楼给的方法
但是还可以相互干扰,如运行到某一语句并改变了一个全局变量的值,
并这个值是决定某函数工作状态的,当某函数内代码运行到此处时,他的工作状态也就随之被改变了
这就是干扰!
!
Global $s_main1 = False, $s_main2 = False 

$i=1
Do        
        If $s_main1 = False Then 
                        $s_main1 = True
                        AdlibRegister("_main1")
                EndIf
                
        If $s_main2 = False Then 
                        $s_main2 = True
                        AdlibRegister("_main2")
                EndIf
                
Until $i=0

func _main1()
        
        AdlibUnRegister("_main1")

        
        ;Sleep(1000) 不要挂起
        ;语句
                
        $s_main1 = False
        
EndFunc

func _main2()
        
        AdlibUnRegister("_main2")
        
        ;Sleep(99999999)
        ;语句
                
        $s_main2 = False
        
EndFunc
发表于 2010-12-17 17:58:49 | 显示全部楼层
本帖最后由 _ddqs. 于 2010-12-17 19:25 编辑

怎么一说说,还真的感觉到"干扰"无处不在,也好象"干扰"也是一种很好的行为
问题是要不要"干扰"
因为有了某某"干扰",才会有了"什么与什么交互"的概念
函数延时运行设置
AdlibRegister("_main1") ;没指定时间,也不会即时执行的,在250毫秒后才会运行
AdlibRegister("_main1",1000) ;延时 1 秒 执行 _main1()
AdlibRegister("_main2",99999999) ;延时 99999999 毫秒 执行 _main2()
若非必要,注意不要在函数语句块内设置Sleep()这会使程序挂起
 楼主| 发表于 2010-12-17 20:56:43 | 显示全部楼层
回复 3# xzxnovice

看了你的代码,不知道怎么应用到我的例子中,
发表于 2010-12-17 21:33:33 | 显示全部楼层
回复 6# leon460


    你最好把代码贴出来,让大家帮你解决干扰呢
 楼主| 发表于 2010-12-17 23:14:42 | 显示全部楼层
回复 7# xzxnovice
说明:在循环中的效果:_main1()的执行是99999999毫秒!实际上我想它不受_main2()
的影响,
$i=1

Do        

        _main1()

        _main2()

Until $i=0

func _main1()

        Sleep(1000)

        ;语句

EndFunc



func _main2()

        Sleep(99999999)

        ;语句

EndFunc
发表于 2010-12-19 21:37:29 | 显示全部楼层
多线程是个困扰已久的问题了
您需要登录后才可以回帖 登录 | 加入

本版积分规则

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

GMT+8, 2024-4-27 18:43 , Processed in 0.080682 second(s), 23 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

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