Jump to content
Sign in to follow this  
Scoox

While 1 WEnd CPU usage?

Recommended Posts

Hi all, since I'm totally new to AutoIT, I guess I'm allowed to ask silly questions. Coming from AutoH*tkey, I thought an effective way to learn AutoIT would be to try and port a few of my own AHK scripts to AutoIT. I'm now looking into how to make scripts "permanent", that is, scripts that continue to run indefinitely until execution is explicitly terminated. From what I've read and seen this is done using something like this:

While 1
WEnd

The above code results in high CPU usage—obviously. Adding a Sleep() to it takes care of the CPU hogging:

While 1
    Sleep(1000)
WEnd

I don't understand, however, why the following code doesn't cause high CPU usage, does execution stop at GuiGetMsg() until something happens on the GUI?:

While 1
    $nMsg = GuiGetMsg()
    ; ... other code ...
WEnd

I just want to know what's considered best practice, thanks!

Share this post


Link to post
Share on other sites

Scoox,

GUIGetMsg has its own built-in Sleep of about 15ms (it varies depending on CPU loading) so you do not need a separate command. A Sleep(10) is more than sufficient to keep any other tight loop from frying the CPU - and as any shorter time parameter is automatically extended to 10ms there is no point in going lower, although tests have proved that even few nanoseconds are sufficient to prevent this.

All clear?

M23


Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites

@Scoox
Hi, and welcome to the AutoIt forum :)
Directly from the Help file about GUIGetMsg():

This function automatically idles the CPU when required so that it can be safely used in tight loops without hogging all the CPU.

 


Click here to see my signature:

Spoiler

Thoughts:

  • I will always thank you for the time you spent for me.
    I'm here to ask, and from your response, I'd like to learn.
    By my knowledge, I can help someone else, and "that someone" could help in turn another, and so on.

/*--------------------------------------------------------------------------------------------------------------------------------------------------------------------------*/

ALWAYS GOOD TO READ:

 

Share this post


Link to post
Share on other sites

Scoox,

Nice question! I'm same new to AutoIt and for a while wondering about this Sleep() tricks in empty While loops.

Melba23 and FrancescoDiMuro,

Thanks for explanations about what going on under the hood!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...