找回密码
 加入
搜索
查看: 15564|回复: 29

[AU3基础] [已解决] 请问,为什么AdlibRegister不执行?

 火.. [复制链接]
发表于 2010-12-15 08:08:31 | 显示全部楼层 |阅读模式
本帖最后由 happytc 于 2010-12-18 20:40 编辑

一段改Total comander软件标题的小代码(如下),为什么一次也不执行呢? 系统是XP
AdlibRegister("TotalCommander")
Exit

Func TotalCommander()
        Local $LeftDir, $RightDir, $ActivePath, $ActiveStatus, $Date, $NewTitle
        $LeftDir = ControlGetText("[CLASS:TTOTAL_CMD]", "", "[CLASS:TPathPanel;INSTANCE:1]")
        $LeftDir = StringReplace($LeftDir, "*.*", ">", -1)
        $RightDir = ControlGetText("[CLASS:TTOTAL_CMD]", "", "[CLASS:TPathPanel;INSTANCE:2]")
        $RightDir = StringReplace($RightDir, "*.*", "", -1)
        $ActivePath = ControlGetText("[CLASS:TTOTAL_CMD]", "", "[CLASS:TMyPanel;INSTANCE:3]")
        $ActivePath = StringReplace($ActivePath, ">", "", -1)
        If $ActivePath = $LeftDir Then 
                $ActiveStatus = ControlGetText("[CLASS:TTOTAL_CMD]", "", "[CLASS:TMyPanel;INSTANCE:5]")
        Else
                $ActiveStatus = ControlGetText("[CLASS:TTOTAL_CMD]", "", "[CLASS:TMyPanel;INSTANCE:8]")
        EndIf
        If StringRight($ActivePath, 1) <> "" Then $ActivePath = $ActivePath & ""
        $Date = @YEAR & "-" & @MON & "-" & @MDAY & "::" & @HOUR & ":" & @MIN & ":" & @SEC 
        $NewTitle = $ActiveStatus & "    " & $ActivePath & "    " & $Date
        WinSetTitle("[CLASS:TTOTAL_CMD]", "", $NewTitle)
EndFunc
没办法,我只好用循环来做,它就执行了,另外问一下,两种方法有什么区别,那种更好?
While 1
        TotalCommander()
        Sleep(250)
WEnd

Func TotalCommander()
        Local $LeftDir, $RightDir, $ActivePath, $ActiveStatus, $Date, $NewTitle
        $LeftDir = ControlGetText("[CLASS:TTOTAL_CMD]", "", "[CLASS:TPathPanel;INSTANCE:1]")
        $LeftDir = StringReplace($LeftDir, "*.*", ">", -1)
        $RightDir = ControlGetText("[CLASS:TTOTAL_CMD]", "", "[CLASS:TPathPanel;INSTANCE:2]")
        $RightDir = StringReplace($RightDir, "*.*", "", -1)
        $ActivePath = ControlGetText("[CLASS:TTOTAL_CMD]", "", "[CLASS:TMyPanel;INSTANCE:3]")
        $ActivePath = StringReplace($ActivePath, ">", "", -1)
        If $ActivePath = $LeftDir Then 
                $ActiveStatus = ControlGetText("[CLASS:TTOTAL_CMD]", "", "[CLASS:TMyPanel;INSTANCE:5]")
        Else
                $ActiveStatus = ControlGetText("[CLASS:TTOTAL_CMD]", "", "[CLASS:TMyPanel;INSTANCE:8]")
        EndIf
        If StringRight($ActivePath, 1) <> "" Then $ActivePath = $ActivePath & ""
        $Date = @YEAR & "-" & @MON & "-" & @MDAY & "::" & @HOUR & ":" & @MIN & ":" & @SEC 
        $NewTitle = $ActiveStatus & "    " & $ActivePath & "    " & $Date
        WinSetTitle("[CLASS:TTOTAL_CMD]", "", $NewTitle)
EndFunc
发表于 2010-12-15 08:25:12 | 显示全部楼层
改成下面的样子应该就可以执行,第一种方法有可能是 Adlib 函数只执行了一次就退出了,看不到效果
 AdlibRegister("TotalCommander")
While 1
        Sleep(250)
WEnd

Func TotalCommander()
        Local $LeftDir, $RightDir, $ActivePath, $ActiveStatus, $Date, $NewTitle
        $LeftDir = ControlGetText("[CLASS:TTOTAL_CMD]", "", "[CLASS:TPathPanel;INSTANCE:1]")
        $LeftDir = StringReplace($LeftDir, "*.*", ">", -1)
        $RightDir = ControlGetText("[CLASS:TTOTAL_CMD]", "", "[CLASS:TPathPanel;INSTANCE:2]")
        $RightDir = StringReplace($RightDir, "*.*", "", -1)
        $ActivePath = ControlGetText("[CLASS:TTOTAL_CMD]", "", "[CLASS:TMyPanel;INSTANCE:3]")
        $ActivePath = StringReplace($ActivePath, ">", "", -1)
        If $ActivePath = $LeftDir Then 
                $ActiveStatus = ControlGetText("[CLASS:TTOTAL_CMD]", "", "[CLASS:TMyPanel;INSTANCE:5]")
        Else
                $ActiveStatus = ControlGetText("[CLASS:TTOTAL_CMD]", "", "[CLASS:TMyPanel;INSTANCE:8]")
        EndIf
        If StringRight($ActivePath, 1) <> "" Then $ActivePath = $ActivePath & ""
        $Date = @YEAR & "-" & @MON & "-" & @MDAY & "::" & @HOUR & ":" & @MIN & ":" & @SEC 
        $NewTitle = $ActiveStatus & "    " & $ActivePath & "    " & $Date
        WinSetTitle("[CLASS:TTOTAL_CMD]", "", $NewTitle)
EndFunc
 楼主| 发表于 2010-12-15 08:53:57 | 显示全部楼层
这样子,那加上这一行:AdlibRegister("TotalCommander")   不是多此一举?

直接用上面的第二种方法不就行了?
若是Adlib 函数只执行了一次就退出了,那它还有啥用处呢?
那au3还有没有相当于AHK里的SetTimer用法呢?
 楼主| 发表于 2010-12-15 08:57:34 | 显示全部楼层
并且我查看了,象我上面的第一种写法,的确一次也没有执行。若是执行了话,TotalCommander软件标题会有变化的。因为我直接把这个函数执行一次,该标题都会变化的
发表于 2010-12-15 09:24:07 | 显示全部楼层
AdlibRegister只是执行指令的函数,又不是程序的挂起函数
主程序没挂起,都退出了,他那有时间和空间再去执行指令
在下面加sleep(9999999999)挂起,你看他还是不是1次(
除非进入函数后被解除
)
发表于 2010-12-15 09:38:20 | 显示全部楼层
AdlibRegister 的重复执行功能 与 while 类同
但作用却不同
由于他的加入并同时注册起用多个函数,这些函数可以协同工作(可以说是并发运行……类似)
前提是函数内不能有被挂起的代码
while 则不能,其运行仅是独占模式,没有退出循环,其他函数就工作不了
但由于消息循环函数的加入,其工作模式又是另一景象。。。

评分

参与人数 1金钱 +30 贡献 +2 收起 理由
afan + 30 + 2

查看全部评分

发表于 2010-12-15 09:54:00 | 显示全部楼层
回复 3# happytc

AdlibRegister要执行的函数不是立即执行的,是在设定时间间隔后才执行第一次的

AdlibRegister虽然不如AHK的计时函数好用,但问题总是有办法解决的
发表于 2010-12-15 10:17:04 | 显示全部楼层
;协同工作示例

Global $true = True


;

AdlibRegister("test")

;--------------------
;用下面循环替代试试看下有没 AdlibRegister("test") 的效果
;--------------------------------------------------------

;While 1
;        test()
;        Sleep(250)
;WEnd

;---------------------------------------------------------


Func test()
        AdlibUnRegister("test")        
        
        If $true Then 
                $true = False
        Else
                $true = True
        EndIf
                
        AdlibRegister("test")        
EndFunc


While 1
        
        ConsoleWrite($true &@LF)
Sleep(251)        
WEnd
发表于 2010-12-15 10:26:23 | 显示全部楼层
AdlibRegister("test") 为什么会协同工作
虽然他是一个循环,但他的循环时间是程序的空闲时间,空闲了,其他代码当然就可以运行
while 则不同,工作中没空闲,sleep了也不是空闲,其他代码也就执行不了~~~
!
发表于 2010-12-15 12:00:29 | 显示全部楼层
又學到了知識。。。
发表于 2010-12-15 12:32:53 | 显示全部楼层
学习了.原来注册函数还要这样用才准确.
 楼主| 发表于 2010-12-16 20:28:25 | 显示全部楼层
非常感谢_ddqs.的知识丰厚,让我学到了不少东西。这种迷惑的东西,真的要有功底才能明白怎么回事呢
 楼主| 发表于 2010-12-16 21:26:13 | 显示全部楼层
_ddqs.给出的例子,的确让人可以深入地明白Adlib函数倒底是如何运行的。

但的确没有AHK的计时函数简单明了易懂。我想问的是au3这样做有什么优点?因为实现象AHK那样的SetTimer也是非常易容的事,把简单的事搞复杂点,总应该有它的优点吧,为什么au3只让Adlib函数执行一次,而不象AHK的SetTimer自动每隔时间就执行呢?
 楼主| 发表于 2010-12-16 21:49:57 | 显示全部楼层
再问一点别的:
是不是HotKeySet函数本身就有Adlib的功能呀?
发表于 2010-12-16 21:59:51 | 显示全部楼层
本帖最后由 飘云 于 2010-12-16 22:01 编辑

回复 13# happytc


    Adlib函数注册了一个用户自定义函数呀,然后隔段时间运行一次这个自定义函数,怎么能说是只运行一次呢?Adlib函数本身是只执行了一次,但是一次就够了,我们需要的是那个自定义函数,如果真的需要,那大不了在自定义函数里再条件判断然后执行一次Adlib函数呗
您需要登录后才可以回帖 登录 | 加入

本版积分规则

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

GMT+8, 2024-4-30 23:06 , Processed in 0.089360 second(s), 24 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

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