找回密码
 加入
搜索
查看: 4032|回复: 8

[GUI管理] 请问在消息循环中加入了sleep(1000)后为什么反应很慢呢?

  [复制链接]
发表于 2013-9-2 16:48:31 | 显示全部楼层 |阅读模式
比如下面的例子
#include <GUIConstantsEx.au3>

Opt('MustDeclareVars', 1)

Example()

;-------------------------------------------------------------------------------------
; Example - Press the button to see the value of the radio boxes
; The script also detects state changes (closed/minimized/timeouts, etc).
Func Example()
        Local $button_1, $group_1, $radio_1, $radio_2, $radio_3
        Local $radioval1, $radioval2, $msg

        Opt("GUICoordMode", 1)
        GUICreate("Radio Box Demo", 400, 280)

        ; Create the controls
        $button_1 = GUICtrlCreateButton("B&utton 1", 30, 20, 120, 40)
        $group_1 = GUICtrlCreateGroup("Group 1", 30, 90, 165, 160)
        GUIStartGroup()
        $radio_1 = GUICtrlCreateRadio("Radio &0", 50, 120, 70, 20)
        $radio_2 = GUICtrlCreateRadio("Radio &1", 50, 150, 60, 20)
        $radio_3 = GUICtrlCreateRadio("Radio &2", 50, 180, 60, 20)

        ; Init our vars that we will use to keep track of GUI events
        $radioval1 = 0    ; We will assume 0 = first radio button selected, 2 = last button
        $radioval2 = 2

        ; Show the GUI
        GUISetState()

        ; In this message loop we use variables to keep track of changes to the radios, another
        ; way would be to use GUICtrlRead() at the end to read in the state of each control
        While 1
                $msg = GUIGetMsg()
                Select
                        Case $msg = $GUI_EVENT_CLOSE
                                MsgBox(0, "", "Dialog was closed")
                                Exit
                        Case $msg = $GUI_EVENT_MINIMIZE
                                MsgBox(0, "", "Dialog minimized", 2)
                        Case $msg = $GUI_EVENT_MAXIMIZE
                                MsgBox(0, "", "Dialog restored", 2)

                        Case $msg = $button_1
                                MsgBox(0, "Default button clicked", "Radio " & $radioval1)

                        Case $msg >= $radio_1 And $msg <= $radio_3
                                $radioval1 = $msg - $radio_1

        EndSelect
        Sleep(1000);加入的延时
        WEnd
EndFunc   ;==>Example
发表于 2013-9-2 18:54:32 | 显示全部楼层
回复 1# ebhb

GUI编程,用事件模式 Opt('GUIOnEventMode',  1)时,才需要在循环里加入SLEEP
你举的例子是消息循环模式,加入SLEEP是多余的.

即使要加SLEEP,也不应当延迟1秒这么长, 设为10 到 200 之间的数字吧.不然就....反应慢或很慢了.
 楼主| 发表于 2013-9-4 10:20:06 | 显示全部楼层
能有什么办法解决吗?怎么换成事件模式?
 楼主| 发表于 2013-9-4 10:35:10 | 显示全部楼层
消息模式和事件模式哪种好点?
发表于 2013-9-4 12:37:17 | 显示全部楼层
消息循环模式时,脚本处于一个死循环中,并且在循环里不断的捕获窗口上产生的消息,然后再根据消息来进行操作。
这种模式是一直在被动的不断获取窗口的消息,在系统资源的耗费上相当的不划算。而且因为脚本的主循环需要不断的处理消息,使得我们想要在主循环中执行其它代码很不方便。因为这样很容易造成消息无法得到及时的响应。

Event 模式时,当窗口产生消息时,才会对消息进行响应,而不会一直处于消息检测状态。这样一来,相较于消息循环模式将会节省大量的 CPU 。
而且在主循环中,我们还可以做其它的事情,而不用担心会造成消息的延迟。(如果在消息循环模式下,主循环也睡眠 3 秒的话,那窗口的消息将会被延迟甚至不响应。)

所以我推荐大家在处理窗口消息时,要尽量使用 Event 模式。
发表于 2013-9-4 20:37:28 | 显示全部楼层
谢2楼的,学习
发表于 2013-9-4 21:09:01 | 显示全部楼层
回复 5# winsphinx
我觉得也是这样,我也经常使用事件模式。
发表于 2013-9-5 22:35:08 | 显示全部楼层
回复 5# winsphinx

原來還有這樣的分別!

又多學了一招降低cpu的用法

 楼主| 发表于 2013-9-5 23:10:27 | 显示全部楼层
试了一下,Event 模式好象也不行
您需要登录后才可以回帖 登录 | 加入

本版积分规则

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

GMT+8, 2024-5-4 02:07 , Processed in 0.077503 second(s), 23 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

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