这段循环代码怎么停止?
本帖最后由 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按扭怎么停止正在循环的任务? 没人回答? 采取GUI事件模式,并且“停止”函数对应一个热键。
经过试验,虽然窗口按钮也能定义对应的事件,但循环中程序似乎不响应控件的事件,而热键100%的响应。 这段只能用“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 本帖最后由 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
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 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 6# 即即
这段代码可行
谢谢! 4# 顽固不化
可行
页:
[1]