Jump to content

For this background task is AdlibRegister or just Sleep better?


Recommended Posts

I'm writing a small prog that will run in the background and sound an alarm if a certain Gmail message arrives in an otherwise unused account.

My inclination is to use a simple While/WEnd loop with a 5 minute Sleep between checking for messages, but I've seen examples here of somewhat similar background tasks that employ AdlibRegister so I'm curious which method is the most reliable and least impactful on the CPU.  Below are two simple scripts that begin to do what I want and represent how I think this could be handled.

While/WEnd with Sleep

#include <CheckMail.au3>
Global $aReturn
Global $iEmails

While 1
    $aReturn = CheckMail("username", "password")
    $iEmails = @extended
    If $iEmails > 0 ExitLoop
    Sleep(300000)   ; check for new messages every 5 minutes
WEnd

;work with contents of $aReturn array that contains the new message(s) found

 

While/WEnd with Sleep and AdlibRegister

#include <CheckMail.au3>
Global $aReturn
Global $iEmails = 0

AdlibRegister('_CallCheckMail',300000) ; check for new messages every 5 minutes

While 1
    Sleep(500000)   ;  I had to put something here. Does higher the value = less CPU demand?
    If $iEmails > 0 ExitLoop
WEnd

;work with contents of $aReturn array that contains the new message(s) found

Func _CallCheckMail
    $aReturn = CheckMail("username", "password")
    $iEmails = @extended
    Return
EndFunc

 

Link to comment
Share on other sites

I know putting sleep in a loop is a good practice just to save cpu cycles.  

The main difference in my use/experience is you can keep running a script while Adlibregister does its thing in the background, almost like running a separate script.  While sleep puts everything on hold in the current script.

 

So if your script had other functions that you wanted to keep active and working Adlibregister would allow this, but if your script only has the one purpose, putting everything to sleep should work just as good and be lighter on resources. 

 

The best may be a combination of both.  Your loop with a small sleep (say 100-500ms) to keep the script "alive" with Adlibregister to call your function at the desired intervals. 

Link to comment
Share on other sites

    So if your script had other functions that you wanted to keep active and working Adlibregister would allow this

The best may be a combination of both.  Your loop with a small sleep (say 100-500ms) to keep the script "alive" with Adlibregister to call your function at the desired intervals. 

Thank you for replying. You make an excellent point about when AdlibRegister is most appropriate.

My second example uses a combination of the two but unlike your suggestion of less than a second I had the Sleep period be 8 minutes.  You wrote "to keep the script "alive". Won't AdlibRegister interrupt Sleep  regardless of the Sleep period? Is there any reason to have the Sleep period be real short vs fairly long (500ms vs 8 minutes), especially when AdlibRegister will cause the interrupt in 5 minutes intervals?

Link to comment
Share on other sites

  • Moderators

timmy2,

We did some tests a good few years ago (someone had developed a method of pausing for very short periods) and found that you needed to pause the CPU for only about 100 nano-seconds to keep the load down - so the minimum useable value of Sleep(10) (10 milli-seconds) should be more than sufficient in your idle loop whichever method you choose.

M23

Edited by Melba23
Got timings wrong

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

 

Link to comment
Share on other sites

Sleep(10) is 10 milliseconds, and 100 nanoseconds sleep isn't possible in AutoIt, it can be done without using the AutoIt Sleep function, but you can't use Sleep to go less than 10 milliseconds.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

timmy2,

We did some tests a good few years ago (someone had developed a method of pausing for very short periods) and found that you needed to pause the CPU for only about 100 nano-seconds to keep the load down - so the minimum useable value of Sleep(10) (10 milli-seconds) should be more than sufficient in your idle loop whichever method you choose.

M23

Thank you for replying, Melba23. From your reply I'm not sure what you mean by "you need to pause the CPU".  Let's put my question this way: since my goal is to check for messages every 5 minutes are there any bad side effects from using Sleep (300000), and if so is there a better solution?

Link to comment
Share on other sites

Thank you for replying, Melba23. From your reply I'm not sure what you mean by "you need to pause the CPU".  Let's put my question this way: since my goal is to check for messages every 5 minutes are there any bad side effects from using Sleep (300000), and if so is there a better solution?

No that's fine. Go ahead with that Sleep().

Link to comment
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
 Share

×
×
  • Create New...