QB64.org Forum

Active Forums => QB64 Discussion => Topic started by: STxAxTIC on March 28, 2022, 07:57:16 am

Title: TIMER vs SLEEP
Post by: STxAxTIC on March 28, 2022, 07:57:16 am
What happens when the unstoppable force of TIMER meets the immovable object of SLEEP?

Code: QB64: [Select]
  1. Screen _NewImage(600, 600, 32)
  2.  
  3. On Timer(t1, .01) moo
  4. Timer(t1) On
  5.  
  6.     PSet (Rnd * _Width, Rnd * _Height), _RGB(255, 255, 255)
  7.     'Sleep
  8.     _Display
  9.  
  10.  
  11. Sub moo
  12.     'do nothing

When there's a Timer at play, SLEEP never fully halts the program - or, it sorta does. It slows the thing down in kindof an interesting way. Not that anybody should ever depend on SLEEP for anything, so it's weird that this question would come up in the first place.... anyway... Thought I'd share.
Title: Re: TIMER vs SLEEP
Post by: bplus on March 28, 2022, 11:04:50 am
Code: QB64: [Select]
  1. On Timer(t1, .01) moo
  ' set at every 100th of a sec doesn't give much time for sleeping.

How many dots can you make in 2 secs? = how many times you press a key in 2 secs.
Code: QB64: [Select]
  1. Screen _NewImage(600, 600, 32)
  2.  
  3. On Timer(t1, 2) moo  ' <<< every 2 secs
  4. Timer(t1) On
  5.  
  6.     PSet (Rnd * _Width, Rnd * _Height), _RGB(255, 255, 255)
  7.     Sleep
  8.  
  9.  
  10. Sub moo
  11.     Cls
  12.  
Title: Re: TIMER vs SLEEP
Post by: Pete on March 28, 2022, 02:54:21 pm
I'd recommend DO: SLEEP: LOOP, but I'd never hear the END of it.

Pete
Title: Re: TIMER vs SLEEP
Post by: bplus on March 28, 2022, 02:56:22 pm
Ha, that's what I do and I don't want to hear the end of it.
Title: Re: TIMER vs SLEEP
Post by: micronoise on March 30, 2022, 07:04:22 pm
I'd recommend DO: SLEEP: LOOP, but I'd never hear the END of it.

Pete

DO:SLEEP:A$=INKEY$:LOOP UNTIL whatever you need A$ to be
Title: Re: TIMER vs SLEEP
Post by: SMcNeill on March 30, 2022, 09:59:07 pm
DO:SLEEP:A$=INKEY$:LOOP UNTIL whatever you need A$ to be

DO
   a$ = INPUT$(1)
LOOP UNTIL a$ = whatever$

Why the need for SLEEP in there?
Title: Re: TIMER vs SLEEP
Post by: bplus on March 30, 2022, 10:08:45 pm
Pete's screwing around and micronoise took him serious.
Title: Re: TIMER vs SLEEP
Post by: SMcNeill on March 30, 2022, 10:17:55 pm
Pete's screwing around and micronoise took him serious.

Pete *NEVER* screws around -- just ask his wife!  Celibate for 50 years now...

It's no wonder she likes me better.  😘
Title: Re: TIMER vs SLEEP
Post by: micronoise on March 31, 2022, 06:29:44 am
Why the need for SLEEP in there?

Reduce CPU usage.
Title: Re: TIMER vs SLEEP
Post by: bplus on March 31, 2022, 06:33:31 am
@micronoise Why do the Do if all you do is sleep? There's the waste!

I'd do it this way:
Code: QB64: [Select]
  1.     'do something
  2.     i = i + 1
  3.     Print i
  4.     Sleep
  5.  
Title: Re: TIMER vs SLEEP
Post by: SMcNeill on March 31, 2022, 07:17:58 am
Reduce CPU usage.

You do realize that SLEEP isn't the only command which limits CPU usage, right?

DO
   a$ = INPUT$(1) <--  this drops CPU usage to almost nothing as well, while waiting for a single key press


Or, just as simple:

DO
   a$ = INKEY$
   _LIMIT 30
LOOP UNTIL a$ = whatever$



SLEEP is honestly a problematic way to go about this type of process, as it's basically nothing more than a DO: _DELAY 0.01: LOOP UNTIL _KEYHIT type process, and there's some old versions of QB64 where SLEEP breaks with unwanted out-of-focus keyhits.  (Such as SHIFT-TAB which should be OS reserved to switch app focus between programs.)

INPUT$(1) works much better for a hard pause, while _LIMIT or _DELAY is better with INKEY$ or _KEYHIT, if you make use of ON TIMER events.

(At least, in my opinion...)
Title: Re: TIMER vs SLEEP
Post by: bplus on March 31, 2022, 07:22:35 am
Sleep let's you look at whatever you've done in 1 loop for however long you like, it's a real advantage for debugging as long as you don't have something on a timer.
Title: Re: TIMER vs SLEEP
Post by: micronoise on March 31, 2022, 10:15:46 am
Maybe a matter of habit ;-) Bear with me, I still consider my self as a newbie. a$=input$(1) is probably better.
Title: Re: TIMER vs SLEEP
Post by: Cobalt on April 01, 2022, 12:13:20 am
There is also this nice little feature:

TIMER(BLAH) OFF

that you can call before SLEEPing

then

TIMER(BLAH) ON

when you wake up
Title: Re: TIMER vs SLEEP
Post by: Pete on April 02, 2022, 06:07:33 pm
Hey Steve, don't make me go Will Smith on you! I'd have my wife handle it, but she was out late last night. Ouch, what's this hay doing in the bed? :D

Pete