4

This loop is very CPU intensive:

While 1
    $msg = GUIGetMsg()
    Switch $msg
        Case $GUI_EVENT_CLOSE
            GUIDelete()
            Exit
        Case $control1
            Func1()
        Case $control2
            Func2()
    EndSwitch
WEnd

This is what I always used. I know there are other ways, but which one is least CPU intensive?

user4157124
  • 2,809
  • 13
  • 27
  • 42
Levi
  • 1,921
  • 1
  • 14
  • 18
  • 1
    Please provide full code that reproduces the issue. That exact code I wrote [a working script for](http://pastie.org/8359519), and didn't see any issue. It's possible that if either `$control1` or `$control2` is `0` then you will have problems, or if `Func1` or `Func2` is CPU intensive, or if there are things working in the background using the Adlib functions. – Matt Sep 27 '13 at 09:00

2 Answers2

5

I ran into this problem using a Switch/Case as well. I thought making code tighter and changing to Select/Case would lower CPU usage. What I ultimately did was trap the script in a Do/Until loop.

This excerpt is from a script which lives in the taskbar and always runs. Users interact by clicking and selecting from the context menu I created. That kicks off a respective function.

While 1
    Do
        $msg = TrayGetMsg()
    Until $msg <> 0
    Select
        Case $msg = $ItemDeviceModel
            DeviceModel()
        Case $msg = $ItemSerial
            SerialNumber()
        Case $msg = $ExitItem
            Exit
    EndSelect
WEnd

In this example the script loops in the quick and easy Do/Until (which is waiting for a user to click the application icon). After the loop is broken the Select/Case runs.

Switch your code with:

While 1
    Do
        $msg = GUIGetMsg()
    Until $msg <> 0
    Switch $msg
        Case $GUI_EVENT_CLOSE
            GUIDelete()
            Exit
        Case $control1
            Func1()
        Case $control2
            Func2()
    EndSwitch
WEnd
user4157124
  • 2,809
  • 13
  • 27
  • 42
Colyn1337
  • 1,655
  • 2
  • 18
  • 27
0

As pointed out by the developer of AutoIt, the "mouse move" event is the second-most called message, so you may want to pre-filter it too, after the "0" message.

#include <GUIConstantsEx.au3>

While 1
    Switch GUIGetMsg()
        Case 0, $GUI_EVENT_MOUSEMOVE
            ContinueLoop
        Case $control1
            Func1()
        Case $control2
            Func2()
    EndSwitch
WEnd


Trying to watch several message sources, namely GUIGetMsg() and TrayGetMsg(), I encountered difficulties because of the ContinueLoop.

But the solution is in fact simple, don't shortcut the loop, just break the switch (remember breaks are implicit in AutoIt):

#include <GUIConstantsEx.au3>

While 1
    Switch GUIGetMsg()
        Case 0, $GUI_EVENT_MOUSEMOVE
            ; break
        Case $control1
            Func1()
        Case $control2
            Func2()
    EndSwitch

    Switch TrayGetMsg()
        Case 0, $GUI_EVENT_MOUSEMOVE
            ; break
        Case $control3
            Func3()
        Case $control4
            Func4()
    EndSwitch
WEnd


(I just checked, TrayGetMsg() does send $GUI_EVENT_MOUSEMOVE messages too)

Gras Double
  • 15,901
  • 8
  • 56
  • 54