Jump to content

An improvised version of the Sleep function


lark
 Share

Recommended Posts

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

  • 1 month later...
  • Moderators

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

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

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