Using SLEEP

New to FreeBASIC? Post your questions here.
Post Reply
jiranz
Posts: 73
Joined: Sep 22, 2006 3:56
Location: New Zealand

Using SLEEP

Post by jiranz »

Is SLEEP duration specific to particular Windows OS whether it counts in seconds or milliseconds?

Code: Select all

SLEEP 1000
For example will this mean a delay of 1000 milli-seconds (1 second) in all and every case or could it mean a delay of 1000 seconds (16+ minutes) in certain OS versions?.
Or does it relate to the manufacturers pc processor per computer?
D.J.Peters
Posts: 8586
Joined: May 28, 2005 3:28
Contact:

Post by D.J.Peters »

try it out :-)

Code: Select all

dim as double tStart=Timer()
Sleep(1000)
dim as double tEnd=Timer()

print tEnd-tStart
sleep
http://www.freebasic.net/wiki/wikka.php ... KeyPgSleep
jiranz
Posts: 73
Joined: Sep 22, 2006 3:56
Location: New Zealand

Post by jiranz »

D.J.Peters wrote:try it out :-)

Code: Select all

dim as double tStart=Timer()
Sleep(1000)
dim as double tEnd=Timer()
print tEnd-tStart
sleep
Didn't work?
Compiler output:
: error 11: Expected constant
Dim As Double tStart=Timer()
^
I want to grant a one sec window to allow for user intervention...
How do i guarantee it is one second and not 1000 seconds in all possible pc scenarios ??

Code: Select all

 PRINT "[SPACEBAR] = reset Codes"
  a$ = ""
  sleep 1000
  a$ = INKEY$:
  IF a$ = CHR$(32) THEN KILL "fields.cfg"
  sleep
Edits:

One of the reasons i wanted to use SLEEP was that the User intervention probably will only happen once per week and i read that sleep will make the 24/7 program consume minimal resources in between usages. So I thought a one second SLEEP would release the resources adequately. But I do not understand if SLEEP will count in seconds or milliseconds on different computers or what factors determine when it does either.

So can anyone tell me if this version of a will release resources when the program is just idling...

Code: Select all

mygo:
  t=fix(timer)+1
  do
   a$=""
   sleep 1
   a$=inkey$
   if a$<>"" then exit do
  loop while t>fix(timer)
  if a$<>"" then
    if a$="1" then print "user intervention program" else print "other 2 etc"
  else
    print "slide on through to main program"
  end if
  goto mygo:
Thanks.
Last edited by jiranz on Jan 07, 2010 23:55, edited 1 time in total.
anonymous1337
Posts: 5494
Joined: Sep 12, 2005 20:06
Location: California

Post by anonymous1337 »

Sleep rests in milliseconds, or until a key is pressed. Pass the value 1 as a second argument to avoid interruption by key press.

Resumes on key press - sleep
Waits .5 seconds (500 milliseconds) or for key press - sleep(500)
Waits .5 seconds (500 milliseconds) - sleep(500, 1)

Sleep is not guaranteed 100% accurate. Each operating system has different timer precision.
"The accuracy of Sleep is variable depending on the OS cycle time (Windows NT/2K/XP: 15 ms, 9x/Me: 50 ms, Linux 10ms, DOS 55 ms)."

-------------------
Make sure to check out the FreeBASIC Wiki.
jiranz
Posts: 73
Joined: Sep 22, 2006 3:56
Location: New Zealand

Post by jiranz »

anonymous1337 wrote:Sleep rests in milliseconds, or until a key is pressed.
The problem is sometimes SLEEP counts in seconds.
So SLEEP 1000 could be 1000 seconds and not single seconds.
see the weblink higher up the page.
If I make a program which could be run on different computers how do I know it will always be in milliseconds?

edit:
My tests indicate the same program compiled as 16 bit counts SLEEP in seconds but in 32 bit as milliseconds.
SO... I was wondering if the problem relates more to the compiler than to factors relating to operating systems? I have search extensively but find only the same confusion I reached. I guess the lesson is to avoid using SLEEP which seems to be a concensus.

So how else to release processor resources in a FB program which idles most of its life but must run 24/7 ? Is there any other way to achieve this goal. (Or is it going to be just too hard for me to do at my beginner level of understanding?)
counting_pine
Site Admin
Posts: 6323
Joined: Jul 05, 2005 17:32
Location: Manchester, Lancs

Post by counting_pine »

FB doesn't have a 16-bit mode!
Also, FB shouldn't produce that "Expected constant" error. Are you sure you're using FB?

If you're using FB, Sleep will be using milliseconds, with the exception that if you're using -lang qb, the dialect with greater QBASIC compatibility, then Sleep will be in seconds like in QB. This can be overruled by adding the second parameter, 0 or 1, to indicate whether the sleep can be interrupted by a keypress.

So, in summary, whether Sleep uses seconds or milliseconds does not depend on the platform. It depends only on whether or not you're in QB-compatibility mode.
Even then, "Sleep 100, 0" will always sleep for 100 milliseconds regardless of platform or dialect.

Just make sure you're using FB!
jiranz
Posts: 73
Joined: Sep 22, 2006 3:56
Location: New Zealand

Post by jiranz »

counting_pine wrote:FB doesn't have a 16-bit mode!
...
Just make sure you're using FB!
Yes I am using FB.
Maybe i should explain that I ran the same code through bc.exe and fbc.exe and got different results for SLEEP.
Also I do not have the SLEEP parameters you refer to?
SYNTAX
SLEEP [milliseconds]
Description
SLEEP will wait until the 'milliseconds' given elapsed (if any value was passed) or until the user presses a key (unless 'milliseconds' is below 100ms).
EXAMPLE
PRINT "press a key"
SLEEP
getkey 'clear the keyboard buffer
PRINT "waiting half second"
SLEEP 500
The accuracy of SLEEP is variable depending on the OS cycle time (Windows NT/2K/XP: 15 ms, 9x/Me: 50 ms, Linux 10ms, DOS 55 ms).
The delay had to be given in seconds in QB
The input buffer is not cleared when using the multi-threading version of the run-time library
(Sorry if i messed up the quote thing. The quote buttons don't seem to like me)
St_W
Posts: 1627
Joined: Feb 11, 2009 14:24
Location: Austria
Contact:

Post by St_W »

Something is wrong with your FreeBasic Compiler.

Check if you use the most recent version (0.20.0b)!

I don't know where you have the information about the sleep function from, but either it is completely outdated or incorrect/incomplete.
See the official wiki page: http://www.freebasic.net/wiki/wikka.php ... KeyPgSleep

I don't want to be rude, but it may be better learning how to download and install programs correctly before programming applications. Advanced knowlegde about computers should be a precondition for programming
jiranz
Posts: 73
Joined: Sep 22, 2006 3:56
Location: New Zealand

Post by jiranz »

Its too late for me to learn about computers! I just "use" them!
But thanks for your comments and no offence taken.

I originally wrote many of my programs in AmigaBasic, later ported them across to VBDOS both of which give SLEEP a one-second minimum count. Since Windows 7 came out, to make them coninue to work I have to recompile them into 32 bit. There are a number of compilers which will do this but after checking out several, I decided to use FB which means modifying some of the code to fit in with the way FB works. In FB, the SLEEP thing counts in milliseconds so i just needed to know if a exe compiled with FB always counts in milliseconds on every computer system everywhere.

I think I have it figured out now that if I compile my code which includes SLEEP using FB that it will always count in milliseconds no matter which computer anywhere. That's because it is how FB manages access to the bios clock system in the pc. I like how FB does it because I had to make special provision in VBDOS in XP because it created a shell which kept its own time and never updated if system bios time changed. My tests show that FB solves that particular issue. Whether it does what it does by accident or design I know not but I like it.

I am working through many program conversions slowly and its quite an enjoyable challenge. Certainly a welcome diversion from the world of pressure business as a holiday project! I have done the jet-ski and wilderness camping thing while the kids grew up and now its just nice to be at home and have family and friends visit, to be able to hand back the grand-kids when I have had enough, and potter around with things like this project.

So, all my conversions to date work satisfactorily but there are still a bunch to go and I will probably ask more apparently "dumb" questions as time goes by!
Thanks for your forebearance in advance!
Skyler
Posts: 242
Joined: Sep 26, 2006 16:30

Post by Skyler »

The answer to your question is yes, at least according to the Wiki. As long as your program is compiled with the "-lang fb" or "-lang fblite" flag, SLEEP will count in milliseconds. Make sure you don't use "-lang qb", the QBasic compatibility mode, or it will count in seconds. ;)

Skyler
counting_pine
Site Admin
Posts: 6323
Joined: Jul 05, 2005 17:32
Location: Manchester, Lancs

Post by counting_pine »

jiranz wrote:Yes I am using FB.
Maybe i should explain that I ran the same code through bc.exe and fbc.exe and got different results for SLEEP.
Also I do not have the SLEEP parameters you refer to?
bc.exe is nothing to do with FreeBASIC. It's probably the QuickBASIC compiler. Don't expect its Sleep to behave the same way.

If you add the second parameter, you will get milliseconds. If it won't allow it, you either have a very old version of FB, or it's not FB.
Post Reply