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

[AU3基础] 如何让一个按钮中正在执行的循环程序,中程停止?

[复制链接]
发表于 2011-8-27 18:29:41 | 显示全部楼层 |阅读模式
先定义一个变量:
Dim $ifstop = 0

按钮1中:
执行一个
for $i=1 to 999999
      .......
     if $ifstop = 1 then
           exitloop
    endif
next

按钮2(停止按钮):
$ifstop = 1

但我只要是点击了按钮1,就停不下来了,怎么按按钮2,程序就是不停止,各位有什么好的方法吗?

评分

参与人数 1金钱 -20 收起 理由
afan -20

查看全部评分

发表于 2011-8-27 18:51:26 | 显示全部楼层
当然停不下来,单线程嘛?

看到这里,我必须得说明下:别又去瞎折腾多线程 - 多进程了。
发表于 2011-8-27 20:40:00 | 显示全部楼层
回答不只一次了
#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

评分

参与人数 2金钱 +30 贡献 +1 收起 理由
llssky2003 + 10 + 1
powerofos + 20

查看全部评分

发表于 2011-8-28 22:31:57 | 显示全部楼层
我觉的也可以使用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
发表于 2022-9-22 01:07:56 | 显示全部楼层
好东东,值得学习。
您需要登录后才可以回帖 登录 | 加入

本版积分规则

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

GMT+8, 2024-5-6 04:08 , Processed in 0.076116 second(s), 24 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

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