Sign in to follow this  
Followers 0
lark

An improvised version of the Sleep function

3 posts in this topic

#1 ·  Posted (edited)

Hi all,

My first uploaded UDF :)

So this is just an improvised version of the Sleep function. It allows the script to sleep, as normal, while still running code. This would be useful if you want your script to sleep for a long period of time, but for your GUI to still be fully functional. So, if you have your script sleep for 5 minutes, and want your GUI to close when you click on the close button, then this will allow you to do it.

I've included the basic switch for the GuiGetMsg() function with the $GUI_EVENT_CLOSE in it, but left everything else out, so that you can customize and edit it to fit your needs.

It takes two parameters: The first one is how long it should wait, in milliseconds, just like the regular sleep function. The second parameter is optional (has a default at 10), and is for how long it should pause while running through its while loop.

Here it is:

Func Sleep2($waitTime, $pauseTime = 10)
Local $finishTime = $waitTime + (@YDAY * 86400000) + (@HOUR * 3600000) + (@MIN * 60000) + (@SEC * 1000) + @MSEC
While 1
  Local $currentTime = (@YDAY * 86400000) + (@HOUR * 3600000) + (@MIN * 60000) + (@SEC * 1000) + @MSEC
  If $currentTime >= $finishTime Then ExitLoop
  $nMsg = GUIGetMsg()
  Switch $nMsg
   Case $GUI_EVENT_CLOSE
    Exit
  EndSwitch
  Sleep($pauseTime)
WEnd
EndFunc

If you want to test it, run this code- it shows a tooltip displaying the current milisecond we are at in the year, then pauses for 5 seconds, and displays the new current time right below it.

$time = (@YDAY * 86400000) + (@HOUR * 3600000) + (@MIN * 60000) + (@SEC * 1000) + @MSEC
ToolTip($time)
Sleep2(5000)
ToolTip($time & @CRLF & ((@YDAY * 86400000) + (@HOUR * 3600000) + (@MIN * 60000) + (@SEC * 1000) + @MSEC))
While 1
Sleep(30)
WEnd

Tell me what you think.

;)

If you want to download it as a file, here it is:

Edited by mischieftoo

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

OK, 2 things at first sight:

1. While executing

Sleep($pauseTime)
your script won't respond to any events.

2. Why not use TimerInit() and TimerDiff().... they are much cheaper as per CPU used.

Edited by GicuPiticu

Share this post


Link to post
Share on other sites

mischieftoo,

In fact you do not need a Sleep($pauseTime) in there at all. GUIGetMsg idles the CPU for you - around 12ms if the CPU is busy, much shorter but still long enough if the CPU is idle. So all you are doing with your additional Sleep is making the script slightly less responsive - which is not what you want. :)

And I would definitely look at using TimerInit() and TimerDiff() to check for $waitTime - they are much more efficient that the complex $finishTime/$currentTime calculations you use at the moment. ;)

M23


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

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  
Followers 0

  • Similar Content

    • ur
      By ur
      Is there anyway to check whether the script is launched already and wait till the other instance is closed and continue after that.
       
    • LOWLIFE
      By LOWLIFE
      This is a personal project to log into several facebook accounts and send a message to a specific person on their birthday. I need to stop a script in a few different ways for several different reasons. For example, it might start sending the email to the wrong account, it might do something other than send a message, it might fail to logout of one account, etc. 
      1) Pause the script at it's exact location with a resume feature in case I need to put it on hold while I do another task. 
      2) Pause the script at it's current location with the option to start over or execute another part of the script (example in GUI with multiple buttons). 
      Also, is there a way to interact with menus? If I log into facebook how can I force it to go straight to sending a message to the proper person? I tried automating a mouseclick but the window opens in different places. I also tried searching for specific text and I couldn't get any information from the windows info tool on the facebook page. 
      Also, how do I get my code in here to show like it does in my editor? (scite)
      Thanks for looking at it, and any help/suggestions I sgreatly appreciated!
      <snip>
    • 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
       
    • Ian_Mac
      By Ian_Mac
      Okey guys , here's the thing, My script is running fine, but when it's already running i cant   turn it off or pause by clicking the on/off button  unless i manually close it on my taskbar. What i wanted to happen is "if i click the ($switch = turn On and Off Button) to Off the script will STOP Running but not closing the entire GUI because i tried to use Exit and ExitLoop but it closes the entire GUI. and i don't want that to happen.  
      BTW. I'm new in AutoIT and practice scripting hope somebody could help me  Godbless. feel free to view my script
      Practice.au3