找回密码
 加入
搜索
查看: 4154|回复: 13

[系统综合] 看了论坛这么多暂停脚本代码,但还是不明白。找了一个英文地址,应该比较清楚。

  [复制链接]
发表于 2013-5-18 02:20:40 | 显示全部楼层 |阅读模式
发表于 2013-5-18 07:40:05 | 显示全部楼层
 楼主| 发表于 2013-5-18 09:36:42 | 显示全部楼层
回复 2# haijie1223
能详细注释一下吗。我用的是一个 while 循环,如果中断 ,我该如何设置
发表于 2013-5-18 09:55:32 | 显示全部楼层
回复 3# nangua111111


    请上代码
 楼主| 发表于 2013-5-18 10:09:15 | 显示全部楼层
 #include <GUIConstantsEx.au3>
 Global $fRunOne = False; Declare a flag声明一个变量标志,随着他的变化中断循环
 
 Opt("GUIOnEventMode", 1);修改特殊的 GUI 函数的返回值类型. 
                         ;0 = (默认) 当窗口被最小化,还原,最大化,改变大小,只通知 
                ;1 = 当窗口发生最小化,最大化,改变大小,就禁止这种事件并立即发出通知,等待自定义函数进行处理.
 
 $hGUI = GUICreate("Test", 500, 500)
 GUISetOnEvent($GUI_EVENT_CLOSE, "_Exit");当一个系统按钮被点击时调用一个用户自定义函数(UDF).
 
 $hButton_1 = GUICtrlCreateButton("Func One", 10, 10, 80, 30)
 GUICtrlSetOnEvent($hButton_1, "_Func_1");为指定控件的点击行为定义一个用户函数.
 $hButton_2 = GUICtrlCreateButton("Func Two", 10, 50, 80, 30)
 GUICtrlSetOnEvent($hButton_2, "_Func_2")

 GUISetState();[可选参数] @SW_SHOW = 使已隐藏窗口显示出来(默认)

 While 1
     Sleep(10)
     ; Check if the flag has been set by the OnEvent function
     If $fRunOne Then
         ; Now start the "real" function from within the main code
         _Func_1_Run()
     EndIf
 WEnd
 
 Func _Func_1()
     ; Set the flag within the OnEvent function
     $fRunOne = True
 EndFunc   ;==>_Func_1
 
 Func _Func_1_Run()
     For $i = 1 To 20
         ConsoleWrite("-Func 1 Running" & @CRLF)
         Sleep(100)
     Next
     ConsoleWrite(">Func 1 Ended" & @CRLF)
     Global $fRunOne = False
 EndFunc   ;==>_Func_1_Run
 
 Func _Func_2()
     For $i = 1 To 3
         ConsoleWrite("+Func 2 Running" & @CRLF)
         Sleep(100)
     Next
     ConsoleWrite(">Func 2 Ended" & @CRLF)
 EndFunc   ;==>_Func_2
 
 Func _Exit()
     Exit
 EndFunc   ;==>_Exit
先讲讲这个,我看了一下代码不明白为何点击button2的时候会暂停, GUICtrlSetOnEvent($hButton_2, "_Func_2")运行函数里面没有涉及到 $fRunOne = True
发表于 2013-5-18 10:49:02 | 显示全部楼层
本帖最后由 haijie1223 于 2013-5-18 10:50 编辑

回复 5# nangua111111


    程序有暂停吗?没有吧,一直在运行,设置一个开关,第一个按钮上只是执行了开关,函数的进行是靠while循环进行的,点击第二个按钮的时候,程序会优先运行控件的通知,然后再返回来。
我把代码改了一下,你可按下第一个按钮,接着按下第二个按钮,然后你会发现两个函数会连续进行,说明两个按钮都起作用了,因为两个按钮只是开关的作用,早已经被执行,你没看到而已,你看到的都是由while完成的。
#include <GUIConstantsEx.au3>
Global $fRunOne = False; Declare a flag声明一个变量标志,随着他的变化中断循环
Global $fRuntwo = False; Declare a flag声明一个变量标志,随着他的变化中断循环
Opt("GUIOnEventMode", 1);修改特殊的 GUI 函数的返回值类型.
$hGUI = GUICreate("Test", 500, 500)
GUISetOnEvent($GUI_EVENT_CLOSE, "_Exit");当一个系统按钮被点击时调用一个用户自定义函数(UDF).
$hButton_1 = GUICtrlCreateButton("Func One", 10, 10, 80, 30)
GUICtrlSetOnEvent($hButton_1, "_Func_1");为指定控件的点击行为定义一个用户函数.
$hButton_2 = GUICtrlCreateButton("Func Two", 10, 50, 80, 30)
GUICtrlSetOnEvent($hButton_2, "_Func_2")
GUISetState();[可选参数] @SW_SHOW = 使已隐藏窗口显示出来(默认)

While 1
        Sleep(10)
        If $fRunOne Then _Func_1_Run()
        If $fRuntwo Then _Func_2_Run()
WEnd

Func _Func_1()
        $fRunOne = True
EndFunc   ;==>_Func_1
Func _Func_2()
        $fRuntwo = True
EndFunc   ;==>_Func_2

Func _Func_1_Run()
        For $i = 1 To 30
                ConsoleWrite($i & @CRLF)
                Sleep(100)
        Next
        ConsoleWrite(">Func 1 Ended" & @CRLF)
        Global $fRunOne = False
EndFunc   ;==>_Func_1_Run


Func _Func_2_Run()
        For $i = 1 To 20
                ConsoleWrite("+Func 2 Running" & @CRLF)
                Sleep(100)
        Next
        ConsoleWrite(">Func 2 Ended" & @CRLF)
        Global $fRuntwo = False
EndFunc   ;==>_Func_2Run

Func _Exit()
        Exit
EndFunc   ;==>_Exit
发表于 2013-5-18 10:53:32 | 显示全部楼层
也就是说控件的通知会比while优先运行~
发表于 2013-5-18 10:54:48 | 显示全部楼层
这样吧,更直观一些
#include <GUIConstantsEx.au3>
Global $fRunOne = False; Declare a flag声明一个变量标志,随着他的变化中断循环
Global $fRuntwo = False; Declare a flag声明一个变量标志,随着他的变化中断循环
Opt("GUIOnEventMode", 1);修改特殊的 GUI 函数的返回值类型.
$hGUI = GUICreate("Test", 500, 500)
GUISetOnEvent($GUI_EVENT_CLOSE, "_Exit");当一个系统按钮被点击时调用一个用户自定义函数(UDF).
$hButton_1 = GUICtrlCreateButton("Func One", 10, 10, 80, 30)
GUICtrlSetOnEvent($hButton_1, "_Func_1");为指定控件的点击行为定义一个用户函数.
$hButton_2 = GUICtrlCreateButton("Func Two", 10, 50, 80, 30)
GUICtrlSetOnEvent($hButton_2, "_Func_2")
GUISetState();[可选参数] @SW_SHOW = 使已隐藏窗口显示出来(默认)

While 1
        Sleep(10)
        If $fRunOne Then _Func_1_Run()
        If $fRuntwo Then _Func_2_Run()
WEnd

Func _Func_1()
        $fRunOne = True
        ConsoleWrite("按钮1开关" & @CRLF)
EndFunc   ;==>_Func_1
Func _Func_2()
        $fRuntwo = True
        ConsoleWrite("按钮2开关" & @CRLF)
EndFunc   ;==>_Func_2

Func _Func_1_Run()
        For $i = 1 To 30
                ConsoleWrite($i & @CRLF)
                Sleep(100)
        Next
        ConsoleWrite(">Func 1 Ended" & @CRLF)
        Global $fRunOne = False
EndFunc   ;==>_Func_1_Run


Func _Func_2_Run()
        For $i = 1 To 20
                ConsoleWrite("+Func 2 Running" & @CRLF)
                Sleep(100)
        Next
        ConsoleWrite(">Func 2 Ended" & @CRLF)
        Global $fRuntwo = False
EndFunc   ;==>_Func_2_Run

Func _Exit()
        Exit
EndFunc   ;==>_Exit

评分

参与人数 1金钱 +10 收起 理由
nangua111111 + 10

查看全部评分

 楼主| 发表于 2013-5-18 11:16:09 | 显示全部楼层
回复 8# haijie1223
我先看看
发表于 2013-5-18 11:19:22 | 显示全部楼层
本帖最后由 shqf 于 2013-5-18 11:21 编辑

楼主,恐怕你对"暂停脚本"的意思理解有误码吧。脚本碰到对话框、消息框等语句如InputBox、MsgBox及winwait类函数才会暂停执行,待一定的条件成就,才会执行下一条语句。而程序没有输出或程序界面没有变化不是脚本暂停。你5楼的代码中没有对话框、消息框等语句winwait类函数,因为有While循环语句,代码一直会运行,会循环执行While循环体内的语句,直到exit终止。一旦产生Button事件,则再去执行相应的函数,执行完相应函数后,再回到循环体内继续执行。如$fRunOne 为真,则执行到_Func_1_Run()函数,否则仅执行到sleep(10)语句。
    你按Button2,执行相应的_Func_2()函数,输出三行“Func 2 Running”,一行“Func 2 Ended”,后回到循环体内,继续循环运行,则是这时再也没有输出而已。你按Button1,$fRunOne 为真,执行到_Func_1_Run()函数,再执行_Func_1(),输出20行“Func 1 Running”,再加一行“Func 1 Ended”,后回到循环体内,继续循环运行。
    在代码开头加上这一句:#AutoIt3Wrapper_Run_Debug_Mode=y,再看看运行结果,你就会明白了。

评分

参与人数 1金钱 +10 收起 理由
nangua111111 + 10

查看全部评分

 楼主| 发表于 2013-5-18 11:59:50 | 显示全部楼层
#AutoIt3Wrapper_Run_Debug_Mode=y

#include <GUIConstantsEx.au3>
$hGUI = GUICreate("Test", 500, 500)
$hButton_1 = GUICtrlCreateButton("Func One", 10, 10, 80, 30)
$hButton_2 = GUICtrlCreateButton("Func Two", 10, 50, 80, 30)
GUISetState();[可选参数] @SW_SHOW = 使已隐藏窗口显示出来(默认)

While 1
        $msg = GUIGetMsg()
        $zanting = False
        Select
                Case $msg = $GUI_EVENT_CLOSE
                                                Exit
                                        
                Case $msg = $hButton_1
                        For $i = 1 To 1355555
                                ConsoleWrite($i & @CRLF)
                                Sleep(5000)
                                If $zanting = True Then ExitLoop
                        Next
                Case $msg = $hButton_2
                        $zanting = Not $zanting
        EndSelect
WEnd
我写的代码,就是用button2去定义 $zanting 值,看了haijie1223 回复视乎明白了很多,但是 如haijie1223 说的 也就是说控件的通知会比while优先运行~我很疑惑这个button2 不是控件通知吗?不知道我理解的对不对
发表于 2013-5-18 13:01:00 | 显示全部楼层
回复 11# nangua111111


换成Event模式~
 楼主| 发表于 2013-5-18 13:36:46 | 显示全部楼层
回复 12# haijie1223
嗯我试试看
 楼主| 发表于 2013-5-18 14:52:03 | 显示全部楼层
本帖最后由 nangua111111 于 2013-5-18 16:50 编辑
#AutoIt3Wrapper_Run_Debug_Mode=y

#include <GUIConstantsEx.au3>
$hGUI = GUICreate("Test", 500, 500)
Opt("GUIOnEventMode", 1)
$hButton_1 = GUICtrlCreateButton("开始", 10, 10, 80, 30)
$hButton_2 = GUICtrlCreateButton("暂停", 10, 50, 80, 30)

GUICtrlSetOnEvent($hButton_1, "kaishi")
GUICtrlSetOnEvent($hButton_2, "zanting")
GUISetOnEvent($GUI_EVENT_CLOSE, "_Exit")
GUISetState()
$zanting = False
While 1
        
        Sleep(10)
        If $zanting = False Then
                For $i = 1 To 1355555
                        ConsoleWrite($i & @CRLF)
                        Sleep(5000)
                If $zanting = True Then ExitLoop
        
        Next
        EndIf
WEnd
Func kaishi()
        $zanting = False
EndFunc   ;==>_Func_kaishi
Func zanting()
        $zanting = True
EndFunc   ;==>_Func_zanting
Func _Exit()
        Exit
EndFunc   ;==>_Exit
这样视乎可行,有没有更简单方式
目的就是为了 If $zanting = True Then ExitLoop这句执行
您需要登录后才可以回帖 登录 | 加入

本版积分规则

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

GMT+8, 2024-5-2 22:13 , Processed in 0.086249 second(s), 24 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

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