PeekMessage( <msec> )

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
User avatar
blueznl
PureBasic Expert
PureBasic Expert
Posts: 6166
Joined: Sat May 17, 2003 11:31 am
Contact:

PeekMessage( <msec> )

Post by blueznl »

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... )
User avatar
Rescator
Addict
Addict
Posts: 1769
Joined: Sat Feb 19, 2005 5:05 pm
Location: Norway

Post by Rescator »

Um! I use Delay(0) for that :)
User avatar
blueznl
PureBasic Expert
PureBasic Expert
Posts: 6166
Joined: Sat May 17, 2003 11:31 am
Contact:

Post by blueznl »

Rescator wrote:Um! I use Delay(0) for that :)
Aw gawd...

< 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... )
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Post by Trond »

WaitMessage_() followed by PeekMessage_()?
User avatar
blueznl
PureBasic Expert
PureBasic Expert
Posts: 6166
Joined: Sat May 17, 2003 11:31 am
Contact:

Post by blueznl »

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. )
( 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... )
User avatar
luis
Addict
Addict
Posts: 3893
Joined: Wed Aug 31, 2005 11:09 pm
Location: Italy

Post by luis »

blueznl wrote: ( Rescator, was that a joke, or does Delay(0) seriously do something? Geez, I'm thick today... sorry. )
"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."

http://msdn.microsoft.com/en-us/library ... S.85).aspx

Delay() -> Sleep() under windows
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Post by Trond »

Blueznl, what you want is SetTimer_() and the functions I mentioned above. To translate it to PB events it needs to be done by the PB team.
User avatar
blueznl
PureBasic Expert
PureBasic Expert
Posts: 6166
Joined: Sat May 17, 2003 11:31 am
Contact:

Post by blueznl »

Little browsing on the net brings me to the next question...
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.
How long is a timeslice?

(Totally off-topic, but I just stumbled over this line in WikiPedia, someone must hate the Amiga pretty badly, read carefully...)
Preemptive 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.
Tsk tsk :-)

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... )
User avatar
luis
Addict
Addict
Posts: 3893
Joined: Wed Aug 31, 2005 11:09 pm
Location: Italy

Post by luis »

blueznl wrote: How long is a timeslice?
It depends on the OS used and usually is tied to the concept of process priority. A higher priority process has a longer timeslice.

There is not a fixed value, I can tell you is not 1 hour !
User avatar
blueznl
PureBasic Expert
PureBasic Expert
Posts: 6166
Joined: Sat May 17, 2003 11:31 am
Contact:

Post by blueznl »

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... )
User avatar
luis
Addict
Addict
Posts: 3893
Joined: Wed Aug 31, 2005 11:09 pm
Location: Italy

Post by luis »

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

Moreover I dont know how the PB Delay() is mapped on other OS.
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Post by Trond »

Delay(0) isn't good because it uses 100% cpu, which drains battery and makes the fan go on.
freak
PureBasic Team
PureBasic Team
Posts: 5940
Joined: Fri Apr 25, 2003 5:21 pm
Location: Germany

Post by freak »

> 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 ?
quidquid Latine dictum sit altum videtur
User avatar
Rescator
Addict
Addict
Posts: 1769
Joined: Sat Feb 19, 2005 5:05 pm
Location: Norway

Post by Rescator »

Trond wrote:Delay(0) isn't good because it uses 100% cpu, which drains battery and makes the fan go on.
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.
> 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 ?
Wondering that myself.

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 :P
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Post by Trond »

Rescator wrote:
Trond wrote:Delay(0) isn't good because it uses 100% cpu, which drains battery and makes the fan go on.
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).
40%+60% is 100%, right? The total CPU use will be 100% with Delay(0).
Post Reply