如何让一个按钮中正在执行的循环程序,中程停止?
先定义一个变量:Dim $ifstop = 0
按钮1中:
执行一个
for $i=1 to 999999
.......
if $ifstop = 1 then
exitloop
endif
next
按钮2(停止按钮):
$ifstop = 1
但我只要是点击了按钮1,就停不下来了,怎么按按钮2,程序就是不停止,各位有什么好的方法吗? 当然停不下来,单线程嘛?
看到这里,我必须得说明下:别又去瞎折腾多线程 - 多进程了。 回答不只一次了
#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
Global $Flg = True
Local $t = 0
#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("Form1", 318, 120, 468, 171)
$Button1 = GUICtrlCreateButton("开始循环", 48, 60, 80, 40)
;$Button2 = GUICtrlCreateButton("结束循环", 208, 60, 80, 40)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###
GUIRegisterMsg($WM_COMMAND, "WM_COMMAND")
While 1
$msg=GUIGetMsg()
Switch $msg
case -3
Exit
Case $Button1
if GUICtrlRead($Button1)="开始循环" and $Flg=True Then
GUICtrlSetData($Button1,"结束循环")
TEST()
Else
GUICtrlSetData($Button1,"开始循环")
EndIf
EndSwitch
WEnd
Func TEST()
While $Flg
ToolTip($t)
$t+=1
WEnd
$Flg=True
$t=0
EndFunc
Func WM_COMMAND($hWnd, $msg, $wParam, $lParam)
#forceref $hWnd, $Msg
Local $nNotifyCode = BitShift($wParam, 16)
Local $nID = BitAND($wParam, 0x0000FFFF)
Local $hCtrl = $lParam
Switch $hCtrl
Case GUICtrlGetHandle($Button1)
if GUICtrlRead($Button1)="结束循环" then
$Flg=False
Else
$Flg=True
EndIf
EndSwitch
EndFunc ;==>WM_COMMAND
我觉的也可以使用hotkeyset 来实现中断:#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
Global $Flg = True
Local $t = 0,$stop
HotKeySet("{ESC}", "Terminate")
#region ### START Koda GUI section ### Form=
$Form1 = GUICreate("Form1", 318, 120, 468, 171)
$Button1 = GUICtrlCreateButton("开始循环", 48, 60, 80, 40)
;$Button2 = GUICtrlCreateButton("结束循环", 208, 60, 80, 40)
GUISetState(@SW_SHOW)
#endregion ### END Koda GUI section ###
GUIRegisterMsg($WM_COMMAND, "WM_COMMAND")
While 1
$msg = GUIGetMsg()
Switch $msg
Case -3
Exit
Case $Button1
If GUICtrlRead($Button1) = "开始循环" And $Flg = True Then
GUICtrlSetData($Button1, "结束循环")
TEST()
Else
GUICtrlSetData($Button1, "开始循环")
EndIf
EndSwitch
WEnd
Func TEST()
While $Flg
ToolTip($t)
$t += 1
If $stop = 1 Then ExitLoop
WEnd
$stop = 0
$Flg = True
$t = 0
EndFunc ;==>TEST
Func WM_COMMAND($hWnd, $msg, $wParam, $lParam)
#forceref $hWnd, $Msg
Local $nNotifyCode = BitShift($wParam, 16)
Local $nID = BitAND($wParam, 0x0000FFFF)
Local $hCtrl = $lParam
Switch $hCtrl
Case GUICtrlGetHandle($Button1)
If GUICtrlRead($Button1) = "结束循环" Then
$Flg = False
Else
$Flg = True
EndIf
EndSwitch
EndFunc ;==>WM_COMMAND
Func Terminate()
$stop = 1
GUICtrlSetData($Button1, "开始循环")
ToolTip("")
EndFunc ;==>Terminate
好东东,值得学习。
页:
[1]