找回密码
 加入
搜索
查看: 3945|回复: 12

[AU3基础] 求助关于死循环

 火.. [复制链接]
发表于 2011-12-3 22:09:59 | 显示全部楼层 |阅读模式
我知道用Opt("GUIOnEventMode",1)事件模型来避免死循环不响应按钮的问题。
现在的问题是,我想要点击一个开始按钮之后就在一个GUI窗口计时,计时函数本身就是一个死循环

Func Count_Time($begin,$Index)
        While 1
           Sleep(1000)
                $dif = TimerDiff($begin)
                _GUICtrlListView_SetItemText($listview, $Index, $dif, 6)
    WEnd
EndFunc

怎么样可以让这个函数进行着,并且可以响应我的其他按钮呢?比如关闭也不行了。
发表于 2011-12-3 23:19:42 | 显示全部楼层
本帖最后由 user3000 于 2011-12-3 23:20 编辑

我会用的是:
假设 $But 是 'Start'
Globe $But_State = True
Globe $begin = ...
Fuc ClickButton()
If $But_State Then
        $But_State = False
        GuictrlSetState($But, 'Stop')
        AdlibRegister(1000, 'Cout_Time')
Else
        $But_State = True
        GuictrlSetState($But, 'Start')
        AdlibUnRegister('Cout_Time')
EndIf
EndFunc
Func Cout_Time()
;$index = ...
$dif = TimerDiff ($begin)
_GuiCtrlListView_SetItemText($listview, $index, $dif, 6)
EndFunc

评分

参与人数 1金钱 +20 收起 理由
afan + 20

查看全部评分

 楼主| 发表于 2011-12-3 23:27:58 | 显示全部楼层
AdlibRegister怎么传参数呢?我是选定了一个ListViewItem之后要修改制定ListViewItem项目的时间值,不过AdlibRegister确实是一个好方法
发表于 2011-12-3 23:30:40 | 显示全部楼层
回复 3# acetaohai123


    可以考虑,  点击按钮后, 就给必要的变量进行赋值吧
发表于 2011-12-4 11:05:34 | 显示全部楼层
学习一下学习一下
发表于 2011-12-4 12:00:17 | 显示全部楼层
可以在子循环中增加对按钮动作的判断
Func Count_Time($begin,$Index)
        While 1
                Sleep(1)
                $CInfo = GUIGetCursorInfo ($Form1)
                If $CInfo[2] = 1 And $CInfo[4]=$Button2 Then Return  ; $Button2停止键ID
                
                $dif = TimerDiff($begin)
                _GUICtrlListView_SetItemText($listview, $Index, $dif, 6)
        WEnd
EndFunc

评分

参与人数 1金钱 +20 收起 理由
afan + 20

查看全部评分

 楼主| 发表于 2011-12-4 12:54:08 | 显示全部楼层
不太理想,哎,还是先看看高手的作品找思路把
发表于 2011-12-4 18:52:20 | 显示全部楼层
可以设置一个开关,循环体放到主程序里
发表于 2011-12-4 18:53:06 | 显示全部楼层
或者WM_COMMAND 消息
发表于 2011-12-5 19:24:15 | 显示全部楼层
#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
Opt("GUIOnEventMode", 1)
#Region ### START Koda GUI section ### Form=
Dim $i=0
$Form1 = GUICreate("Form1", 415, 155, 192, 124)
GUISetOnEvent($GUI_EVENT_CLOSE, "Form1Close")

$Label1 = GUICtrlCreateLabel("Label1", 48, 16, 316, 17)

$Button1 = GUICtrlCreateButton("开始/停止", 88, 64, 75, 25)
GUICtrlSetOnEvent(-1, "Button1Click")
$Button2 = GUICtrlCreateButton("点我试试", 176, 64, 75, 25)
GUICtrlSetOnEvent(-1, "Button2Click")
GUISetState(@SW_SHOW)
$begin=TimerInit()
#EndRegion ### END Koda GUI section ###

While 1
        Sleep(100)
        If $i=1 Then

        $dif = TimerDiff($begin)
        GUICtrlSetData($Label1,$dif)
        EndIf
WEnd

Func Button1Click()
If $i=0 Then
$i=1
Else
        $i=0
        EndIf
EndFunc
Func Button2Click()
MsgBox(0,0,"试试")
EndFunc
Func Form1Close()
Exit
EndFunc

评分

参与人数 1金钱 +20 收起 理由
afan + 20

查看全部评分

发表于 2011-12-6 08:30:42 | 显示全部楼层
我知道用Opt("GUIOnEventMode",1)事件模型来避免死循环不响应按钮的问题。
现在的问题是,我想要点击一个开 ...
acetaohai123 发表于 2011-12-3 22:09


这个是au3的硬伤,不能多线程。
虽然可以跳出死循环,但一旦跳出了,原循环就暂停了,而你举的例子里循环又是不能暂停的时间,虽然可以用AdlibRegister勉强应对一下,但这样就造成计时不准确了。
发表于 2011-12-6 09:11:35 | 显示全部楼层
这个是au3的硬伤,不能多线程。
虽然可以跳出死循环,但一旦跳出了,原循环就暂停了,而你举的例子里循 ...
happytc 发表于 2011-12-6 08:30


-----------------------------------------
不知道想达到怎样的准确性?
是不受电脑系统时间影响的独立时间?
能不能这样,在退出原循环的条件触发时,记录原循环时间与即时电脑系统时间的差值
当原循环再次启动时,再捕捉即时电脑系统时间,然后加上差值。
或者退出原循环时,启动一个定时器来计数....

评分

参与人数 1金钱 +20 收起 理由
afan + 20

查看全部评分

发表于 2011-12-7 21:37:52 | 显示全部楼层
学习一下学习一下
学习一下学习一下
您需要登录后才可以回帖 登录 | 加入

本版积分规则

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

GMT+8, 2024-9-20 20:28 , Processed in 0.104860 second(s), 25 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

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