找回密码
 加入
搜索
查看: 3861|回复: 4

[GUI管理] [已解决][自问自答][方便搜索]执行死循环导致GUI按键不响应的解决方案

[复制链接]
发表于 2011-4-18 03:19:17 | 显示全部楼层 |阅读模式
本帖最后由 easefull 于 2011-4-18 03:38 编辑

貌似,这已经是AutoIt的经典GUI问题了.
原贴地址:http://www.autoitx.com/forum.php?mod=viewthread&tid=16334

示例:
#include <GUIConstantsEx.au3>

$Form1  = GUICreate("游戏", 351, 271, 192, 124)
$Label1 = GUICtrlCreateLabel("---", 32, 64, 80, 20)
$Button1 = GUICtrlCreateButton("开始", 112, 16, 43, 25)
$Button4 = GUICtrlCreateButton("停止", 180, 16, 43, 25)
GUISetState(@SW_SHOW)
Global $new = 0
Local $msg
While 1
        $msg = GUIGetMsg()
        Select
                Case $msg = $GUI_EVENT_CLOSE
                        ExitLoop
                Case $msg = $Button1 ;开始
                        $i = 1
                        _Change()
                Case $msg = $Button4 ;停止
                        $i = 0
                        _Change()
                        
        EndSelect
WEnd
GUIDelete()
Func _Change()
    While $i
                        $new = $new + Random(-10,10,1)
                        GUICtrlSetData($Label1,$new)
                        Sleep(1000)
                        If ( $i = 0 ) Then
                                ExitLoop
            EndIf
        WEnd
EndFunc
 楼主| 发表于 2011-4-18 03:21:50 | 显示全部楼层
本帖最后由 easefull 于 2011-4-18 03:33 编辑

消息模式的解决方案
作者:水木子
来源:http://www.autoitx.com/forum.php ... amp;fromuid=7652895
$Form1 = GUICreate("游戏", 350, 280)
$Label1 = GUICtrlCreateLabel("---", 32, 64, 80, 20)
$Button1 = GUICtrlCreateButton("开始", 112, 16, 43, 25)
$Button4 = GUICtrlCreateButton("停止", 180, 16, 43, 25)
GUISetState()

While 1
        $msg = GUIGetMsg()
        Select
                Case $msg = -3
                        ExitLoop
                Case $msg = $Button1 ;开始
                        Dim $new
                        AdlibRegister('_Change', 1000)
                Case $msg = $Button4 ;停止
                        AdlibUnRegister('_Change')
                        MsgBox(0, '', '已停止!')
        EndSelect
WEnd

Func _Change()        
        $new = $new + Random(-10, 10, 1)
        GUICtrlSetData($Label1, $new)
EndFunc   ;==>_Change
 楼主| 发表于 2011-4-18 03:24:25 | 显示全部楼层
本帖最后由 easefull 于 2011-4-18 03:34 编辑

事件模式的解决方案
作者:风行者
来源:http://www.autoitx.com/forum.php ... amp;fromuid=7652895
#include <GUIConstantsEx.au3>
Opt("GUIOnEventMode",1)
$Form1 = GUICreate("游戏", 351, 271, 192, 124)
GUISetOnEvent($GUI_EVENT_CLOSE,"close")
$Label1 = GUICtrlCreateLabel("---", 32, 64, 80, 20)
$Button1 = GUICtrlCreateButton("开始", 112, 16, 43, 25)
GUICtrlSetOnEvent(-1,"button1")
$Button4 = GUICtrlCreateButton("停止", 180, 16, 43, 25)
GUICtrlSetOnEvent(-1,"button4")
GUISetState(@SW_SHOW)
Global $i = False
While 1
    Local $new
        If $i = True Then
        $new = $new + Random(-10,10,1)
        GUICtrlSetData($Label1,$new)
        EndIf
        Sleep(1000)  
WEnd
GUIDelete()

Func close()
        Exit
EndFunc

Func button1()
        $i = True
EndFunc

Func button4()
        $i = False
EndFunc
发表于 2011-8-11 20:56:02 | 显示全部楼层
这个经典呀,波垂下来
发表于 2011-12-23 20:31:41 | 显示全部楼层
看不懂。。。。。。。
您需要登录后才可以回帖 登录 | 加入

本版积分规则

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

GMT+8, 2024-9-21 10:59 , Processed in 0.078489 second(s), 22 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

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