PeekMessage( <msec> )
PeekMessage( <msec> )
Halts the program and returns timeslices to Windows, until there is a message in the queue, but does not take the message out of the queue.
( PB6.00 LTS Win11 x64 Asrock AB350 Pro4 Ryzen 5 3600 32GB GTX1060 6GB)
( The path to enlightenment and the PureBasic Survival Guide right here... )
( The path to enlightenment and the PureBasic Survival Guide right here... )
Aw gawd...Rescator wrote:Um! I use Delay(0) for that
< quickly starting up PureBasic and checking the help file >
Nope, I think that's not the same thing... Unless Delay() is not properly documented...
WaitForQueueNoLongerEmpty( <msec> ) is such a horrible way to write this, but perhaps it's more what I intend. Or perhaps n = CountUnprocessedEvents( <msec> ). Hard to explain, this one

( PB6.00 LTS Win11 x64 Asrock AB350 Pro4 Ryzen 5 3600 32GB GTX1060 6GB)
( The path to enlightenment and the PureBasic Survival Guide right here... )
( The path to enlightenment and the PureBasic Survival Guide right here... )
What I'm looking for is a WaitWindowEvent() equivalent which does leave the message in the queue, yes, I can code around it, I know 
It's to avoid those typical Delay() instructions that people throw all over the place to allow Windows (TM) to 'do its thing'. Hmm. I finally found the proper explanation!
Think of it as a Delay with a timeout!
Regular Delay( n ) waits for n millisecs, but it doesn't care if there are a zillion messages in the queue or not.
I'd like a similar command DelayOnNoEvent( n ) which waits for n millisecs UNLESS there are messages in the queue, in which case it doesn't wait.
Ah, I think I've finally expressed myself with the fine nuance such an intricate complex request deserves... (Excuse me whilst I look for a dark and damp corner to repeatedly flog myself, it's one of *those* days...)
( Rescator, was that a joke, or does Delay(0) seriously do something? Geez, I'm thick today... sorry. )

It's to avoid those typical Delay() instructions that people throw all over the place to allow Windows (TM) to 'do its thing'. Hmm. I finally found the proper explanation!

Think of it as a Delay with a timeout!
Regular Delay( n ) waits for n millisecs, but it doesn't care if there are a zillion messages in the queue or not.
I'd like a similar command DelayOnNoEvent( n ) which waits for n millisecs UNLESS there are messages in the queue, in which case it doesn't wait.
Ah, I think I've finally expressed myself with the fine nuance such an intricate complex request deserves... (Excuse me whilst I look for a dark and damp corner to repeatedly flog myself, it's one of *those* days...)
( Rescator, was that a joke, or does Delay(0) seriously do something? Geez, I'm thick today... sorry. )
( PB6.00 LTS Win11 x64 Asrock AB350 Pro4 Ryzen 5 3600 32GB GTX1060 6GB)
( The path to enlightenment and the PureBasic Survival Guide right here... )
( The path to enlightenment and the PureBasic Survival Guide right here... )
"A value of zero causes the thread to relinquish the remainder of its time slice to any other thread of equal priority that is ready to run. If there are no other threads of equal priority ready to run, the function returns immediately, and the thread continues execution."blueznl wrote: ( Rescator, was that a joke, or does Delay(0) seriously do something? Geez, I'm thick today... sorry. )
http://msdn.microsoft.com/en-us/library ... S.85).aspx
Delay() -> Sleep() under windows
Little browsing on the net brings me to the next question...
(Totally off-topic, but I just stumbled over this line in WikiPedia, someone must hate the Amiga pretty badly, read carefully...)

Anyway, looks like Delay(0) in combination with a timer does the job. A bit more work, but it will do.
How long is a timeslice?A value of zero causes the thread to relinquish the remainder of its time slice to any other thread of equal priority that is ready to run. If there are no other threads of equal priority ready to run, the function returns immediately, and the thread continues execution.
(Totally off-topic, but I just stumbled over this line in WikiPedia, someone must hate the Amiga pretty badly, read carefully...)
Tsk tskPreemptive multitasking is a rare example of an advanced feature of the Amiga operating system which was not found on other desktops of a similar price range during the heyday of the Amiga and as a consequence supporters of the Amiga system tend to focus an emphasis upon its importance and in doing so tend to overlook deficiencies in the Amiga computer system which led to unresolved doubts surrounding its absolute usefulness as a professional computing platform and ultimately to its downfall.

Anyway, looks like Delay(0) in combination with a timer does the job. A bit more work, but it will do.
( PB6.00 LTS Win11 x64 Asrock AB350 Pro4 Ryzen 5 3600 32GB GTX1060 6GB)
( The path to enlightenment and the PureBasic Survival Guide right here... )
( The path to enlightenment and the PureBasic Survival Guide right here... )
Yeah, what I decided to do is use a timer and Delay(0)... Is Delay(0) universal or does it just apply to Windows?
( PB6.00 LTS Win11 x64 Asrock AB350 Pro4 Ryzen 5 3600 32GB GTX1060 6GB)
( The path to enlightenment and the PureBasic Survival Guide right here... )
( The path to enlightenment and the PureBasic Survival Guide right here... )
I know under linux there is a yield() or sched_yield() to achieve the same effect... I don't recall if sleep(0) do the same on linux.blueznl wrote:Yeah, what I decided to do is use a timer and Delay(0)... Is Delay(0) universal or does it just apply to Windows?
Moreover I dont know how the PB Delay() is mapped on other OS.
Actually Delay(0) prevents using 100% CPU, with Delay(0) the loop will use whatever CPU is available, so it might only use 40% if something else is using 60% (like a background process). A bit oversimplified, there are other scheduling criteria in an OS. The important thing is it prevents messing up the multitasking especially on single core systems.Trond wrote:Delay(0) isn't good because it uses 100% cpu, which drains battery and makes the fan go on.
Wondering that myself.> until there is a message in the queue, but does not take the message out of the queue.
I'm curious: Why would you want to wait for an event and then not handle it ?
Then again I got no idea what program tis is but personally I've used
WaitWindowEvent(1000)
for a lazy GUI, and WaitWindowEvent(1)
if I need it to be very responsive.
This does exactly what you want by the sound of it, it wait's (n) ms unless there is a message, in which case it breaks the timeout at once.
Best of all you can use WaitWindowEvent(timeout) a variable that you adjust.
One app of mine (GridStream Player) uses 1ms but when it's hidden/minimized it uses 1000ms.
PS! If you use Delay() (not sure how the WaitWindowEvent does things but)
using timeBeginPeriod_() and timeEndPeriod_() allows you to improve the update rate from 10ms'ish on the NT kernel to almost 1ms precision.
Setting these affect Delay() (and Sleep_() and timeGetTime_() precision)
ElapsedMilliseconds() is not affected as it uses getTicks or similar.
One thing is for sure, regardless how you make a loop and what functions/methods you use.
If the user is not moving the mouse or using the keyboard and if the program is not doing any work then the CPU use of that program and it's loop really should be 0% CPU, if it's not then the loop is eating precious CPU cycles.
It's a shame there isn't more mention of this in the manual as many newbies tend to make horrible loops in the beginning. I sure did

40%+60% is 100%, right? The total CPU use will be 100% with Delay(0).Rescator wrote:Actually Delay(0) prevents using 100% CPU, with Delay(0) the loop will use whatever CPU is available, so it might only use 40% if something else is using 60% (like a background process).Trond wrote:Delay(0) isn't good because it uses 100% cpu, which drains battery and makes the fan go on.