找回密码
 加入
搜索
查看: 16480|回复: 38

复杂的 while循环 退出问题?【已完美解决,见最后一楼】

  [复制链接]
发表于 2008-12-12 10:39:38 | 显示全部楼层 |阅读模式
 
#include <GUIConstants.au3>
$Form1 = GUICreate("例子一", 307, 81, -1, -1)
$Button1 = GUICtrlCreateButton("开始", 32, 24, 97, 33, 0)
$Button2 = GUICtrlCreateButton("停止", 176, 24, 97, 33, 0)
GUISetState(@SW_SHOW)
While 1
$nMsg = GUIGetMsg()
Switch $nMsg
    Case $GUI_EVENT_CLOSE
              Exit
    Case $Button1
              Dim $i=0,$j=1
              While $j
              $i += 1
              Sleep(800)
      WEnd
Case $Button2
         $j=0
  MsgBox(0,"结果",$i)
EndSwitch
WEnd

;怎么才能退出这个循环 我按BOTTON2没有用
;然后我用了pooker的解决方案



While 1
$nMsg = GUIGetMsg()
Switch $nMsg
    Case $GUI_EVENT_CLOSE
              Exit
    Case $Button1
              Dim $i=0,$j=1
              While $j
              $i += 1
              sleep(800)
;我这里比pooker的解决方案多了sleep(800),因为我的原函数里,这个位置有好多操作所以用sleep代替
              $nMsg = GUIGetMsg() ;捕获窗口消息
              Switch $nMsg ;判断窗口信息
                   Case $GUI_EVENT_CLOSE ;点击退出"X"
                            Exit
                   Case $Button2 ;点击"停止"
                            $j=0
              EndSwitch
              WEnd
    MsgBox(0,"结果",$i)
EndSwitch
WEnd

;同样无法解决问题,
;然后我用事件模式_____________

 
#include <GUIConstants.au3>
Opt('GUIOnEventMode', 1)
$Form1 = GUICreate("例子一", 307, 81, -1, -1)
GUICtrlSetOnEvent($GUI_EVENT_CLOSE,'example')
$Button1 = GUICtrlCreateButton("开始", 32, 24, 97, 33, 0)
$Button2 = GUICtrlCreateButton("停止", 176, 24, 97, 33, 0)
GUISetState(@SW_SHOW)
GUICtrlSetOnEvent($Button1,'exam')
GUICtrlSetOnEvent($Button2,'exam2')
While 1
sleep(1000)
WEnd
Func example()
Exit
EndFunc
Func exam()
$j = Not $j
While $j
$i += 1
Sleep(800)
WEnd
MsgBox(0,"结果",$i)
EndFunc
Func exam2()
$j = 0
MsgBox(0,"结果",$i)
EndFunc


;问题依然出现。我现在的解决方案就是用hotkey


 
#include <GUIConstants.au3>
Opt('GUIOnEventMode', 1)
HotKeySet('{F9}','exam')
Global $j,$i
$Form1 = GUICreate("例子一", 307, 81, -1, -1)
GUICtrlSetOnEvent($GUI_EVENT_CLOSE,'example')
$Button1 = GUICtrlCreateButton("开始", 32, 24, 97, 33, 0)
$Button2 = GUICtrlCreateButton("停止", 176, 24, 97, 33, 0)
GUISetState(@SW_SHOW)
GUICtrlSetOnEvent($Button1,'example')
GUICtrlSetOnEvent($Button2,'example')
While 1
sleep(1000)
WEnd
Func example()
Switch @GUI_CtrlId
    Case $GUI_EVENT_CLOSE
   Exit
    Case $Button1
           exam()
Case $Button2
         $j=0
  MsgBox(0,"结果",$i)
EndSwitch
EndFunc
Func exam()
$j = Not $j
While $j
$i += 1
Sleep(800)
WEnd
MsgBox(0,"结果",$i)
EndFunc



;现在退出循环的问题解决了,又出现新的问题,
;1.hotkey是系统级的热键设定,跟其他软件造成冲突
;2.botton2还是起不到作用
主要原因就是我的这个while ...wend中间有sleep(800)本来用pooker的方案可以很好的解决,
我的原函数里面运行的程序时间大于sleep(800)

;哪位高手来解答这个困惑我好久的用gui控件退出While循环问题


[ 本帖最后由 superflq 于 2008-12-21 08:57 编辑 ]

评分

参与人数 2金钱 +52 贡献 +14 收起 理由
zps26 + 30 + 5
lynfr8 + 22 + 9 好问题!

查看全部评分

发表于 2008-12-12 11:15:34 | 显示全部楼层

ExitLoop
 楼主| 发表于 2008-12-12 11:20:18 | 显示全部楼层
原帖由 mexiaoyuoo 于 2008-12-12 11:15 发表

ExitLoop



这个语句放在哪里呢
发表于 2008-12-12 11:37:46 | 显示全部楼层
放在你想退出循环的语句后
发表于 2008-12-12 11:50:39 | 显示全部楼层

Dim $i
$Form1 = GUICreate("例子一", 307, 81, -1, -1)
$Button1 = GUICtrlCreateButton("开始", 32, 24, 97, 33, 0)
$Button2 = GUICtrlCreateButton("停止", 176, 24, 97, 33, 0)
GUISetState(@SW_SHOW)
While 1
        $nMsg = GUIGetMsg()
        Switch $nMsg
                Case -3
                        Exit
                Case $Button1
                        $i = 0
                        AdlibEnable('cont', 800)
                Case $Button2
                        AdlibDisable()
                        MsgBox(0, "结果", $i)
        EndSwitch
WEnd

Func cont()
        $i += 1
EndFunc

评分

参与人数 1金钱 +1 收起 理由
superflq + 1 狮子大哥还有没其他办法,exitloop的话要怎 ...

查看全部评分

 楼主| 发表于 2008-12-12 12:02:39 | 显示全部楼层
原帖由 liongodmien 于 2008-12-12 11:50 发表
[au3]
Dim $i
$Form1 = GUICreate("例子一", 307, 81, -1, -1)
$Button1 = GUICtrlCreateButton("开始", 32, 24, 97, 33, 0)
$Button2 = GUICtrlCreateButton("停止", 176, 24, 97, 33, 0)
GUISetState(@SW_SHO ...



我的原函数里面运行的需要的程序时间远大于sleep(800),而且是多个程序
也不只有一个botton1,好多个botton

[ 本帖最后由 superflq 于 2008-12-12 12:04 编辑 ]
 楼主| 发表于 2008-12-12 12:04:52 | 显示全部楼层
原帖由 waynelue 于 2008-12-12 11:37 发表
放在你想退出循环的语句后



能写 下么
发表于 2008-12-12 12:37:48 | 显示全部楼层
虽然没有运行 但在我看来 pooker 的方法是有效的
可能是你所写代码的其他部分造成了不能正常响应

hotkeyset 也是有效的解决方案

所说的exitloop 添加位置 指的是 替换 $button2 的 $j = 1
然后把包含
while $j;将$j改为1
$nmsg = GUIGetMsg()
switch $nmsg
case $nmsg = $button2
exitloop
endswitch
wend
发表于 2008-12-12 12:39:02 | 显示全部楼层
对于pooker的方法 请确认是无效 还是 反映比较慢
如果是反应比较慢 可能是sleep(800)的时间间隔太长了
 楼主| 发表于 2008-12-12 13:11:51 | 显示全部楼层
原帖由 文白 于 2008-12-12 12:39 发表
对于pooker的方法 请确认是无效 还是 反映比较慢
如果是反应比较慢 可能是sleep(800)的时间间隔太长了



pooker的方法,是反映时间慢,sleep(800)是代替我要运行的程序,那些程序有运行时间,不可能变短忽略
pooker方法的原型是没有sleep()的,随时都可以GUIGetMsg()
 楼主| 发表于 2008-12-12 13:13:36 | 显示全部楼层
原帖由 文白 于 2008-12-12 12:37 发表
虽然没有运行 但在我看来 pooker 的方法是有效的
可能是你所写代码的其他部分造成了不能正常响应

hotkeyset 也是有效的解决方案

所说的exitloop 添加位置 指的是 替换 $button2 的 $j = 1
然后把包含
while ...





#include <GUIConstants.au3>
$Form1 = GUICreate("例子一", 307, 81, -1, -1)
$Button1 = GUICtrlCreateButton("开始", 32, 24, 97, 33, 0)
$Button2 = GUICtrlCreateButton("停止", 176, 24, 97, 33, 0)
GUISetState(@SW_SHOW)
While 1
$nMsg = GUIGetMsg()
Switch $nMsg
    Case $GUI_EVENT_CLOSE
              Exit
    Case $Button1
              Dim $i=0
              While 1
              $i += 1
              Sleep(800)
      WEnd
Case $Button2
       exitloop
  MsgBox(0,"结果",$i)
EndSwitch 
WEnd

经过我测试,也是无效的,
用hotkey是也没有办法的办法,很容易跟其他软件造成冲突
看看除了hotkey有没更好的解决方法

[ 本帖最后由 superflq 于 2008-12-12 13:17 编辑 ]
发表于 2008-12-12 13:44:48 | 显示全部楼层

#include <GUIConstants.au3>
$Form1 = GUICreate("例子一", 307, 81, -1, -1)
$Button1 = GUICtrlCreateButton("开始", 32, 24, 97, 33, 0)
$Button2 = GUICtrlCreateButton("停止", 176, 24, 97, 33, 0)
GUISetState(@SW_SHOW)
While 1
$nMsg = GUIGetMsg()
Switch $nMsg
    Case $GUI_EVENT_CLOSE
              Exit
    Case $Button1
              Dim $i=0
              While 1
if GUIGetMsg() = $Button2 then exitloop
              $i += 1
              Sleep(800)
      WEnd
  MsgBox(0,"结果",$i)
EndSwitch 
WEnd
 楼主| 发表于 2008-12-12 13:54:56 | 显示全部楼层
原帖由 文白 于 2008-12-12 13:44 发表
[au3]
#include
$Form1 = GUICreate("例子一", 307, 81, -1, -1)
$Button1 = GUICtrlCreateButton("开始", 32, 24, 97, 33, 0)
$Button2 = GUICtrlCreateButton("停止", 176, 24, 97, 33, 0)
GUISetState(@SW_ ...



似乎也不行,要按好多下菜行,我发现问题了,
当按下botton2是,语句正好运行在sleep(800),
一个循环下来当语句运行到if GUIGetMsg() = $Button2 then exitloop的时候
已经松开botton2了,必需当语句运行到if GUIGetMsg() = $Button2 then exitloop的时候botton正好按下才行
这个方案其实跟POOKER的方案一样,都不能完美退出,有延时的循环。
发表于 2008-12-12 14:01:25 | 显示全部楼层
有要求也是好的!

$Form1 = GUICreate("例子一", 307, 81, -1, -1)
$Button1 = GUICtrlCreateButton("开始", 32, 24, 97, 33, 0)
$Button2 = GUICtrlCreateButton("停止", 176, 24, 97, 33, 0)
GUISetState(@SW_SHOW)
While 1
        $nMsg = GUIGetMsg()
        Switch $nMsg
                Case -3
                        Exit
                Case $Button1
                        Dim $i = 0, $j = 1, $T = TimerInit()
                        While $j
                                If TimerDiff($T) >= 800 Then
                                        $T = TimerInit()
                                        $i += 1
                                EndIf
                                Switch GUIGetMsg()
                                        Case -3
                                                Exit
                                        Case $Button2
                                                $j = 0
                                EndSwitch
                        WEnd
                        MsgBox(0, "结果", $i)
        EndSwitch
WEnd

评分

参与人数 1金钱 +10 贡献 +5 收起 理由
大绯狼 + 10 + 5 又一个经典问题。

查看全部评分

发表于 2008-12-12 14:04:52 | 显示全部楼层
无论是事件模式还是消息模式,一定要让程序能有空闲时间获得你的动作消息,不然就像电脑假死,没反应!
您需要登录后才可以回帖 登录 | 加入

本版积分规则

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

GMT+8, 2024-5-17 02:28 , Processed in 0.079459 second(s), 20 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

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