vivier001 发表于 2009-11-25 00:50:17

GUI在函数执行的时候无法正常退出问题

本帖最后由 vivier001 于 2009-11-25 14:01 编辑

在OnEvent模式下像下面的程序
如果按下确定键也就是执行"OKButton"函数的时候。
当"OKButton"正在执行的时候点击GUI主窗口右上角的退出键好像没反应啊。
程序还是会一直执行不会退出。
也就是按下确定键后,主窗口右上角退出键失效了。

怎样才能在一个函数执行的时候退出这个程序呢?#include <GUIConstantsEx.au3>

Opt("GUIOnEventMode", 1); 切换为 OnEvent 模式
$mainwindow = GUICreate("您好,世界", 200, 100) ; 创建窗口并返回窗口句柄
GUISetOnEvent($GUI_EVENT_CLOSE, "CLOSEClicked") ; 设置窗口关闭事件

$okbutton = GUICtrlCreateButton("确定", 70, 50, 60)
GUICtrlSetOnEvent($okbutton, "OKButton") ; 设置按钮控件单击事件
GUISetState(@SW_SHOW)

While 1
Sleep(1000); 不做任何事
WEnd

Func OKButton()
        While 1
                sleep(2000)
        WEnd
EndFunc

Func CLOSEClicked()
        Exit
EndFunc

afan 发表于 2009-11-25 01:43:29

可以使用热键,或#include <GUIConstantsEx.au3>

Opt("GUIOnEventMode", 1) ; 切换为 OnEvent 模式
$mainwindow = GUICreate("您好,世界", 200, 100) ; 创建窗口并返回窗口句柄
GUISetOnEvent($GUI_EVENT_CLOSE, "CLOSEClicked") ; 设置窗口关闭事件

$okbutton = GUICtrlCreateButton("确定", 70, 50, 60)
GUICtrlSetOnEvent($okbutton, "OKButton") ; 设置按钮控件单击事件
GUISetState(@SW_SHOW)

While 1
        Sleep(1000) ; 不做任何事
WEnd

Func OKButton()
        Opt("GUIOnEventMode", 0)
        While 1
                If GUIGetMsg() = -3 Then CLOSEClicked()
        WEnd
EndFunc   ;==>OKButton

Func CLOSEClicked()
        Exit
EndFunc   ;==>CLOSEClicked

afan 发表于 2009-11-25 01:47:19

回复 1# vivier001


    建议LZ修改为较详细的标题,便于搜索

vivier001 发表于 2009-11-25 14:01:08

可以使用热键,或
afan 发表于 2009-11-25 01:43 http://www.autoitx.com/images/common/back.gif


    那要是在guigetmsg()后 ,有一个长时间要运行的函数。比如加上一个sleep(200000)而又不想用热键退出,怎么办#include <GUIConstantsEx.au3>

Opt("GUIOnEventMode", 1) ; 切换为 OnEvent 模式
$mainwindow = GUICreate("您好,世界", 200, 100) ; 创建窗口并返回窗口句柄
GUISetOnEvent($GUI_EVENT_CLOSE, "CLOSEClicked") ; 设置窗口关闭事件

$okbutton = GUICtrlCreateButton("确定", 70, 50, 60)
GUICtrlSetOnEvent($okbutton, "OKButton") ; 设置按钮控件单击事件
GUISetState(@SW_SHOW)

While 1
      Sleep(1000) ; 不做任何事
WEnd

Func OKButton()
      Opt("GUIOnEventMode", 0)
      While 1
                If GUIGetMsg() = -3 Then CLOSEClicked()
                sleep(200000);;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;这样
      WEnd
EndFunc   ;==>OKButton

Func CLOSEClicked()
      Exit
EndFunc   ;==>CLOSEClicked

afan 发表于 2009-11-25 14:21:06

本帖最后由 afan 于 2009-11-25 14:22 编辑

长时间要运行动作并不就是sleep(),sleep已经休眠了~
最好有个长时间运行的实例,再看怎样处理比较好~

hzxymkb 发表于 2009-11-25 14:21:59

楼上的几位朋友真是高手!
我也遇到这样的问题!解决了!!谢谢楼主发这个帖!

vivier001 发表于 2009-11-25 16:06:38

本帖最后由 vivier001 于 2009-11-25 16:25 编辑

长时间要运行动作并不就是sleep(),sleep已经休眠了~
最好有个长时间运行的实例,再看怎样处理比较好~
afan 发表于 2009-11-25 14:21 http://www.autoitx.com/images/common/back.gif


好像这样就行了,首先采用的是opt ("GUIOnEventMode",0)模式,
然后点击按钮后设置opt ("GUIOnEventMode",1)然后退出while之前
加上opt ("GUIOnEventMode",0)#include <GUIConstantsEx.au3>

Opt("GUIOnEventMode", 0)
$mainwindow = GUICreate("您好,世界", 200, 100)
GUISetOnEvent($GUI_EVENT_CLOSE, "CLOSEClicked")

$okbutton = GUICtrlCreateButton("确定", 70, 50, 60)
GUICtrlSetOnEvent($okbutton, "OKButton")
GUISetState(@SW_SHOW)

While 1
      Sleep(1000)
WEnd

Func OKButton()
      Opt("GUIOnEventMode", 1)
      While 1
                If GUIGetMsg() = -3 Then CLOSEClicked()
                                $oHTTP = ObjCreate("winhttp.winhttprequest.5.1")
                $oHTTP.Open("post",".......",false)
                $oHTTP.setRequestHeader("Cache-Control", "no-cache")
                $oHTTP.setRequestHeader("Content-Type","application/x-www-form-urlencoded")
                $oHTTP.Send("")
                                If ... Then
                                        opt ("GUIOnEventMode",0)
                                        ExitLoop
                                EndIf
                               
                                $oHTTP.Open("post",".......",false)
                $oHTTP.setRequestHeader("Cache-Control", "no-cache")
                $oHTTP.setRequestHeader("Content-Type","application/x-www-form-urlencoded")
                $oHTTP.Send("")
               If ... Then
                        opt ("GUIOnEventMode",0)
                        ExitLoop
                EndIf
    .........
      WEnd
EndFunc   ;==>OKButton

Func CLOSEClicked()
      Exit
EndFunc   ;==>CLOSEClicked

afan 发表于 2009-11-25 16:22:39

回复 7# vivier001


    这个我是没办法了,那个延时不好干预,函数内部好像都是等待网页加载完才返回,等高手来帮你解答吧

vivier001 发表于 2009-11-25 16:29:34

回复vivier001


    这个我是没办法了,那个延时不好干预,函数内部好像都是等待网页加载完才返回, ...
afan 发表于 2009-11-25 16:22 http://www.autoitx.com/images/common/back.gif


    看看7楼我修改过的,差不多能满足条件了,不过好像容易导致程序停止响应,要稍等一会才好

vivier001 发表于 2009-11-25 16:34:29

回复vivier001


    这个我是没办法了,那个延时不好干预,函数内部好像都是等待网页加载完才返回, ...
afan 发表于 2009-11-25 16:22 http://www.autoitx.com/images/common/back.gif


    看看7楼我修改过的,差不多能满足条件了,不过好像容易导致程序停止响应,要稍等一会才好
页: [1]
查看完整版本: GUI在函数执行的时候无法正常退出问题