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

这段循环代码怎么停止?

[复制链接]
发表于 2009-7-18 10:13:41 | 显示全部楼层 |阅读模式
本帖最后由 yingce 于 2009-7-19 18:56 编辑

#include <GUIConstants.au3>
HotKeySet("{F2}", "ExitProgram")

#Region ### START Koda GUI section ### Form=
$AForm1 = GUICreate("AForm1", 206, 83, 780, 16)
$Label1 = GUICtrlCreateLabel("次数:", 18, 20, 44, 17)
$Input1 = GUICtrlCreateInput("5", 56, 16, 57, 21)
$Button1 = GUICtrlCreateButton("开始", 120, 14, 67, 25, 0)
$Button2 = GUICtrlCreateButton("停止", 120, 48, 67, 25, 0)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###





While 1
        $nMsg = GUIGetMsg()
        Switch $nMsg
                Case $GUI_EVENT_CLOSE
                        Exit
                Case $Button1
                Run('NOTEPAD.exe')
                Sleep(500)
                        kaishi()
                Case $Button2
                ExitLoop

        EndSwitch
WEnd

Func kaishi()
For $x = 1 to (GUICtrlRead($Input1))
Dim $Num = ""
        For $i = 1 To 4
        $Num = $Num & Chr(Random(Asc("0"), Asc("z"), 1)) ;Random(97, 122, 1) 也可替换为 Random(Asc("a"), Asc("z"), 1)
        Next
WinActivate("无标题 - 记事本")
send($Num)
Sleep(600)
send("{enter}")
Next
EndFunc
Exit


Func ExitProgram()
        Exit
EndFunc

button2按扭怎么停止正在循环的任务?
 楼主| 发表于 2009-7-18 11:00:30 | 显示全部楼层
没人回答?
发表于 2009-7-18 12:14:03 | 显示全部楼层
采取GUI事件模式,并且“停止”函数对应一个热键。
经过试验,虽然窗口按钮也能定义对应的事件,但循环中程序似乎不响应控件的事件,而热键100%的响应。
发表于 2009-7-18 12:28:55 | 显示全部楼层
这段只能用“esc”键停止,没达到你的要求,不知是否属于3.3.1.1的bug
#include <GUIConstants.au3>
Opt("GUIOnEventMode", 1)
HotKeySet("{F2}", "ExitProgram")
HotKeySet("{esc}", "stop")
Global $isStop = False
#Region ### START Koda GUI section ### Form=
$AForm1 = GUICreate("AForm1", 206, 83, 780, 16)
GUISetOnEvent($GUI_EVENT_CLOSE, "ExitProgram")
$Label1 = GUICtrlCreateLabel("次数:", 18, 20, 44, 17)
$Input1 = GUICtrlCreateInput("15", 56, 16, 57, 21)
$Button1 = GUICtrlCreateButton("开始", 120, 14, 67, 25, 0)
GUICtrlSetOnEvent($button1, "kaishi") 
$Button2 = GUICtrlCreateButton("停止", 120, 48, 67, 25, 0)
GUICtrlSetOnEvent($button2, "stop") 
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

While 1
        Sleep(100)
WEnd

Func kaishi()
        $isStop = False
        Run('NOTEPAD.exe')
        Sleep(500)
        For $x = 1 To (GUICtrlRead($Input1))
                If $isStop = True Then
                        $isStop = False
                        Return
                EndIf
                Dim $Num = ""
                For $i = 1 To 4
                        $Num = $Num & Chr(Random(Asc("0"), Asc("z"), 1)) ;Random(97, 122, 1) 也可替换为 Random(Asc("a"), Asc("z"), 1)
                Next
                WinActivate("无标题 - 记事本")
                Send($Num)
                Sleep(600)
                Send("{enter}")
        Next
EndFunc   ;==>kaishi


Func stop()
        $isStop = True
EndFunc   ;==>stop

Func ExitProgram()
        Exit
EndFunc   ;==>ExitProgram
发表于 2009-7-18 14:28:48 | 显示全部楼层
本帖最后由 caodongchun 于 2009-7-18 15:07 编辑

你这段代码停止=退出,加入AdlibEnable即可!
如果某些函数还在进行中,要终止他,只能使用比他优先级更高的函数,比如热键,TrayOnEventMode,AdlibEnable 均可达到你的要求,下面的实例就是用了AdlibEnable,感觉上好像是gui循环模式生效,而实际是AdlibEnable检测起到了作用!

#include <GUIConstants.au3>
HotKeySet("{F2}", "ExitProgram")

#Region ### START Koda GUI section ### Form=
$AForm1 = GUICreate("AForm1", 206, 83, 780, 16)
$Label1 = GUICtrlCreateLabel("次数:", 18, 20, 44, 17)
$Input1 = GUICtrlCreateInput("5", 56, 16, 57, 21)
$Button1 = GUICtrlCreateButton("开始", 120, 14, 67, 25, 0)
$Button2 = GUICtrlCreateButton("停止", 120, 48, 67, 25, 0)
GUISetState(@SW_SHOW)
AdlibEnable('stop',20)
#EndRegion ### END Koda GUI section ###





While 1
        $nMsg = GUIGetMsg()
        Switch $nMsg
                Case $GUI_EVENT_CLOSE
                        Exit
                Case $Button1
                Run('NOTEPAD.exe')
                Sleep(500)
                        kaishi()
                Case $Button2
                ExitLoop
        EndSwitch
WEnd

Func stop()
        If GUIGetMsg() = $Button2 Then Exit
EndFunc

        

Func kaishi()
For $x = 1 to (GUICtrlRead($Input1))
Dim $Num = ""
        For $i = 1 To 4
        $Num = $Num & Chr(Random(Asc("0"), Asc("z"), 1)) ;Random(97, 122, 1) 也可替换为 Random(Asc("a"), Asc("z"), 1)
        Next
WinActivate("无标题 - 记事本")
send($Num)
Sleep(600)
send("{enter}")
Next
EndFunc
Exit


Func ExitProgram()
        Exit
EndFunc
发表于 2009-7-19 09:21:13 | 显示全部楼层
4# 顽固不化
#include <GUIConstants.au3>
Opt("GUIOnEventMode", 1)
Global $isStop = False, $Stop = False
#Region ### START Koda GUI section ### Form=
$AForm1 = GUICreate("AForm1", 206, 83, 780, 16)
GUISetOnEvent($GUI_EVENT_CLOSE, "stop")
$Label1 = GUICtrlCreateLabel("次数:", 18, 20, 44, 17)
$Input1 = GUICtrlCreateInput("15", 56, 16, 57, 21)
$Button1 = GUICtrlCreateButton("开始", 120, 14, 67, 25, 0)
GUICtrlSetOnEvent($Button1, "stop")
$Button2 = GUICtrlCreateButton("停止", 120, 48, 67, 25, 0)
GUICtrlSetOnEvent($Button2, "stop")
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

While 1
        If $isStop = True Then
                If Not ProcessExists("NOTEPAD.exe") Then Run('NOTEPAD.exe')
                Sleep(500)
                For $x = 1 To(GUICtrlRead($Input1))
                        If $Stop = True Then
                                $isStop = False
                                $Stop = False
                                ExitLoop
                        Else
                                Dim $Num = ""
                                For $i = 1 To 4
                                        $Num = $Num & Chr(Random(Asc("0"), Asc("z"), 1)) ;Random(97, 122, 1) 也可替换为 Random(Asc("a"), Asc("z"), 1)
                                Next
                                WinActivate("无标题 - 记事本")
                                Send($Num)
                                Sleep(600)
                                Send("{enter}")
                        EndIf
                Next
        EndIf
        Sleep(50)
WEnd


Func stop()
        If @GUI_CtrlId = $Button1 Then $isStop = True
        If @GUI_CtrlId = $Button2 Then $Stop = True
        If @GUI_CtrlId = $GUI_EVENT_CLOSE Then Exit
EndFunc   ;==>stop
发表于 2009-7-19 09:26:31 | 显示全部楼层
1# yingce
#include <GUIConstants.au3>
HotKeySet("{F2}", "ExitProgram")

#Region ### START Koda GUI section ### Form=
$AForm1 = GUICreate("AForm1", 206, 83, 780, 16)
$Label1 = GUICtrlCreateLabel("次数:", 18, 20, 44, 17)
$Input1 = GUICtrlCreateInput("5", 56, 16, 57, 21)
$Button1 = GUICtrlCreateButton("开始", 120, 14, 67, 25, 0)
$Button2 = GUICtrlCreateButton("停止", 120, 48, 67, 25, 0)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###


While 1
        $nMsg = GUIGetMsg()
        Switch $nMsg
                Case $GUI_EVENT_CLOSE
                        Exit
                Case $Button1
                        Run('NOTEPAD.exe')
                        Sleep(500)
                        kaishi()
                Case $Button2
                        ;ExitLoop
        EndSwitch
WEnd

Func kaishi()
        For $x = 1 to(GUICtrlRead($Input1))
                Dim $Num = ""
                For $i = 1 To 4
                        If GUIGetMsg() = $Button2 Then ExitLoop 2
                        $Num = $Num & Chr(Random(Asc("0"), Asc("z"), 1)) ;Random(97, 122, 1) 也可替换为 Random(Asc("a"), Asc("z"), 1)
                Next
                WinActivate("无标题 - 记事本")
                send($Num)
                Sleep(600)
                send("{enter}")
        Next
EndFunc   ;==>kaishi
Exit


Func ExitProgram()
        Exit
EndFunc   ;==>ExitProgram
 楼主| 发表于 2009-7-19 14:26:15 | 显示全部楼层
6# 即即


这段代码可行
谢谢!
 楼主| 发表于 2009-7-19 14:28:16 | 显示全部楼层
4# 顽固不化


可行
您需要登录后才可以回帖 登录 | 加入

本版积分规则

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

GMT+8, 2024-12-23 19:35 , Processed in 0.072666 second(s), 19 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

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