Jump to content
timmy2

For this background task is AdlibRegister or just Sleep better?

Recommended Posts

timmy2

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

 

Share this post


Link to post
Share on other sites
ViciousXUSMC

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. 

Share this post


Link to post
Share on other sites
timmy2

    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?

Share this post


Link to post
Share on other sites
Melba23

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

Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______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
ViciousXUSMC

I suppose not, I just figured that if you're going to use a long sleep why use Adlibregister() at all just use sleep.

Share this post


Link to post
Share on other sites
BrewManNH

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

Share this post


Link to post
Share on other sites
timmy2

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?

Share this post


Link to post
Share on other sites
AdmiralAlkex

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().

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

  • Similar Content

    • Bilgus
      By Bilgus
      Apparently you can't use a function with variables for AdlibRegister
      Like you declare it with a variable even an optional one and it throws an error if you try and call it
      Thats ok though I figured out a hack using eval to get a better result
      Registered_Funct(250);Register Sleep(10000) Registered_Funct(-1);UnRegister Func Registered_Funct($iTime = -1) Local $i_Time = Eval("iTime") If $i_Time > 0 Then AdlibRegister("Registered_Funct", $i_Time) ElseIf $i_Time < 0 Then AdlibUnRegister("Registered_Funct") Else ConsoleWrite("Do Stuff") EndIf EndFunc
      Surely it has an impact on efficiency though...
    • Miliardsto
      By Miliardsto
      Is there any difference in timers and AdlibRegister ?
      in :
      performance usage reliability or any kind of difference I want use few timers at once so it will make difference.
      I have problem with timers and I saw AdlibRegister behaves similar like:
      wont stop crashing slowing program so much maybe is there any trick to make it better?
    • badcoder123
      By badcoder123
      Hey everyone,
      I've been messing around with some new things and adlibs look extremely useful/interesting, however, I can't seem to get it to work.. Here's what I have
      HotKeySet("{F1}", "_Exit") $qCount = 1 Global $_Timer AdlibRegister($_Timer, 1000) AdlibUnRegister($_Timer) While 1 SoundPlay(@WindowsDir & "\media\tada.wav", 1) $qCount += 1 ToolTip('"Tada" has been played ' & $qCount & " times",200,200) WEnd Func _Timer() Local Static $iCount += 1 ConsoleWrite($iCount) If $iCount = 20 Then ConsoleWrite("iCount is at " & $iCount) EndFunc Func _Exit() Exit EndFunc Basically I want it to call the _Timer function but it doesn't seem to work :/ any ideas?
    • Nareshm
      By Nareshm
      I have script Like This :
      Opt("TrayAutoPause",0)
      $var = Ping("www.google.com")
      If not @error Then
      WinActivate ( "testapp")
      ControlClick ( "testapp", "", "[Savetest]")
      ControlSend ( " :: Gujarat TPDS  - eFPS :: Version - 2.2.0.0  - [eFPS Bill]", "", "[NAME:btnSavebyFP]", "{enter}")
      WinWait(":: Aadhar/EID/Verification Number Entry ::")
      ControlClick(":: Aadhar/EID/Verification Number Entry ::", "", "[NAME:btnNext]")
      If WinExists ("Aadhar : Length Check") Then
         ControlClick("Aadhar : Length Check", "", "[CLASS:Button; INSTANCE:1]")
         ControlSend(":: Aadhar/EID/Verification Number Entry ::", "", "[NAME:txtAadharNumber]", "{backspace}")
         ControlSend(":: Aadhar/EID/Verification Number Entry ::", "", "[NAME:txtAadharNumber]", "^v")
         ControlClick(":: Aadhar/EID/Verification Number Entry ::", "", "[NAME:btnNext]")
         WinWait(" :: Barcoded Ration Card Members - Biometric Verification/Check Utility ::")
         ControlSend("[Class:WindowsForms10.Window.8.app.0.378734a]", "", "[NAME:PictureBox2]", "{tab} {enter}")
         Sleep(5600)
         RunWait("rasdial /disconnect", "", @SW_HIDE) ; Stop connect from start
         RunWait("rasdial Internet", "", @SW_HIDE) ; Connect again
         Exit
      EndIf
      WinWait(" :: Barcoded Ration Card Members - Biometric Verification/Check Utility ::", "", 1)
      ControlSend("[Class:WindowsForms10.Window.8.app.0.378734a]", "", "[NAME:PictureBox2]", "{tab} {enter}")
      Sleep(5600)
      RunWait("rasdial /disconnect", "", @SW_HIDE) ; Stop connect from start
      RunWait("rasdial Internet", "", @SW_HIDE) ; Connect again
      Exit
      Else
         #include <MsgBoxConstants.au3>
         MsgBox($MB_TOPMOST, "Connection Status", "Internet is not Connected")
      Exit
      Endif
    • Nareshm
      By Nareshm
      How can i add sleep command using internet speed.
      If my internet speed is 40-50Kbps then sleep for 5 seconds,
      If my internet speed is 30-40Kbps then sleep for 4 seconds,
      If my internet speed is 20-30Kbps then sleep for 3 seconds.

      *Sleep command used bitween my costom script, Example,
      RunWait("rasdial /disconnect", "", @SW_HIDE) ; Stop connect from start
      Sleep(5000)
      RunWait("rasdial Internet", "", @SW_HIDE) ; Connect again
       
×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.