Jump to content

Sleep function delay timer


Recommended Posts

Documentation says the sleep function delay value is in milliseconds, but in this case it appears to be more like centiseconds. This code displays the message box after a little more than 5 seconds, instead of .5 seconds. When I change 500 to 700, it takes a little over 7 seconds.

$Count = 0

While $Count < 500

$Count += 1

Sleep(1)

WEnd

MsgBox(0, "", $Count & " loops")

What am I missing?

Link to comment
Share on other sites

  • Moderators

jp123,

Welcome to the AutoIt forum. ;)

For reasons to do with the internals of the Windows API, any Sleep value of less that 10 is treated as if it were about 10-12 ms. If you want really short Sleeps then this thread might be of use to you. :)

Just for completeness, Sleep(0) is a special case which just misses the next timeslice for the process - which can be anything from instantaneous to a few ms. :)

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

The minimum sleep value is 10 ms.

Undocumented but true. ;)

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

All makes sense now. I don't actually need the 1ms sleep, 100ms should work. I'm looking for the results from "$PingTime = Ping("www.google.com",5000)" in order to spend a limited amount of time determining if there's an internet connection when the script first runs. Checking that every tenth of a second seems reasonable. The script downloads a few images from the web - the GUI is unresponsive until it's done downloading (or times out), and I don't want the user to think the program is frozen if they don't have an internet connection, so I have to check that first an not try to download any images if it's not even possible.

Thanks for the tip!

Link to comment
Share on other sites

Seems I don't quote understand how this works - I didn't intend to "make" the GUI unresponsive, I was under the impression that AutoIT had to wait for the image to download (or timeout trying) before moving on to the next step.

The way it is now, if there were a "Cancel" button, the user couldn't click it until the image were done downloading (or timed out trying)... I thought AutoIt had to do one thing at a time - are you saying I can have it try to download the image and allow user interaction simultaneously?

Link to comment
Share on other sites

I have never tried, but I imagine it should be possible. I don't know how you are downloading the files. You certainly can stop the ping easily enough by testing after every sleep of 100 ms. This is an excellent tutorial: Interrupting a Running Function

Perhaps I'm wrong about interrupting a download. It would be easier if we could see more code. Use the AutoIt code tags. Don't paste into the popup window: add the tags to the post and then paste your code between the code tags.

Edited by czardas
Link to comment
Share on other sites

The way it is now, if there were a "Cancel" button, the user couldn't click it until the image were done downloading (or timed out trying)... I thought AutoIt had to do one thing at a time - are you saying I can have it try to download the image and allow user interaction simultaneously?

You can do whatever you want with a little bit of imagination and a sledgehammer ;)

Background downloading is easy. If you are using InetGet() see the background parameter. If you use something else, tell us so we can suggest something based on that.

Link to comment
Share on other sites

I've tried this from several different angles using examples from other posts & suggestions above, still no luck. What I really need to accomplish is to reliably determine whether or not the client running the script has an active internet connection, within 3 seconds of when the script runs. Ping function takes 15 seconds to timeout, no matter what I set the timeout to. A DllCall to wininet.dll does not report the correct information - it says my internet connection is active even when the network cable is unplugged. Both InetGet() and _INetGetSource() take just as long as Ping does to come back with nothing. Trying to use a While loop containing Sleep to test a variable at a certain interval based on any of the above hasn't worked because the loop doesn't start until the methods above return their condition, which always takes more than the desired 3 seconds when there's no internet connection.

@AdmiralAlkex - background downloading apparently doesn't work in this situation because it appears that the download has to start before it can "move" to the background and let something else happen. I'm probably wrong about the specifics, but I'm not wrong that it doesn't work when my network cable is unplugged.

@czardas - Interrupting a running function doesn't work because when I put Ping (or anything else) into a function to try to interrupt it, it just freezes that function until timeout.

After everything I've seen AutoIt do so simply & efficiently, it just seems like there should be some way to get this off the ground. Test internet connection state in 3 seconds max.

Edited by jp123
Link to comment
Share on other sites

You know what? I don't know which thread to respond to, this one or the ping timeout thread. You shouldn't start a new topic relating to the same issues when people are trying to help you in the first thread.

Connection to a router is not the same as connection to the internet. The timeout will occur after waiting for the return ping if it dosn't arrive within the alloted time. This is in case you have a particularly slow connection. Your router probably takes longer than three seconds to establish a connection: I know mine, and probably many other people's, does.. What's all this three seconds business anyway? Explain why you need such accuracy for this.

Regarding interrupting your script - I don't believe you have tried the sledgehammer approach yet. No code, so nobody has a clue what you tried or why it failed.

Edited by czardas
Link to comment
Share on other sites

I'm looking for the results from "$PingTime = Ping("www.google.com",5000)" in order to spend a limited amount of time determining if there's an internet connection when the script first runs.

Why not just look at your network adapter status?

Spoiler

“Hello, ladies, look at your man, now back to me, now back at your man, now back to me. Sadly, he isn’t me, but if he stopped using ladies scented body wash and switched to Old Spice, he could smell like he’s me. Look down, back up, where are you? You’re on a boat with the man your man could smell like. What’s in your hand, back at me. I have it, it’s an oyster with two tickets to that thing you love. Look again, the tickets are now diamonds. Anything is possible when your man smells like Old Spice and not a lady. I’m on a horse.”

 

Link to comment
Share on other sites

@czardas:

I started the other ping timeout thread in hopes that there was another "undocumented" way to work with Ping that would point me in the right direction - like there is with Sleep (the answer that started this whole extended conversation). It's a related issue, but not the same question, and if I can firgure out the ping problem everything else is a non-issue.

I realize that connection to a router is not the same as connection to the internet, but in my case it seems that the router isn't part of the issue becuase when I test with "no internet connection", I'm physically unplugging the cable from my computer. I need the responsiveness of the GUI to be the same regardless of OS or network condition.

"The timeout will occur after waiting for the return ping if it dosn't arrive within the alloted time." - The alloted time is what I'm trying to control. No matter what I specify for the ping timeout, it takes my machine 15 seconds for ping to return 0, and we're getting into the topic of the other thread, so I'm just commenting on your comment here ;)

Three seconds is a specification from the client. They don't want the user to have to wait for more than three seconds to interact with the GUI.

I'm not at my dev machine right now, will post some code later - in a new thread.

Edited by jp123
Link to comment
Share on other sites

Why not just look at your network adapter status?

Thanks for the suggestion - some of the clients running this script may be connected to a LAN but not the internet, so the network adapter status might be OK.

I tried _WinAPI_IsInternetConnected with WinAPIEx_3.7_3380, and even with the network cable unplugged is reported back that everything was OK, not sure why.

And to top it all off, many of the clients will be running XP, even some still on Windows 2000, so even if _WinAPI_IsInternetConnected worked I couldn't use it (says it requires Vista or later).

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

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...