Scoox Posted September 3, 2018 Share Posted September 3, 2018 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! Tersion 1 Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted September 3, 2018 Moderators Share Posted September 3, 2018 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 SkysLastChance and Tersion 1 1 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 columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area Link to comment Share on other sites More sharing options...
FrancescoDiMuro Posted September 3, 2018 Share Posted September 3, 2018 @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. Tersion 1 Click here to see my signature: Spoiler ALWAYS GOOD TO READ: Forum Rules Forum Etiquette Link to comment Share on other sites More sharing options...
Tersion Posted September 3, 2018 Share Posted September 3, 2018 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! Link to comment Share on other sites More sharing options...
Scoox Posted September 3, 2018 Author Share Posted September 3, 2018 @Melba23 thanks for the insightful answer, and @FrancescoDiMuro for reminding me to RTFM Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now