Wait function not using any recources 
Author Message
 Wait function not using any recources

I'm searching a function that i can call to wait for
some seconds and which is not using any CPU-time.

A simple loop with the 'doevents' command an checking
the the system time uses 100% of CPU-Time.
Now I am using the timer control-element to call a subroutine
at 1 second interval for example and then counting the elapsed
time.
Does anyone know a better/easyer way to do this ?

Matthias G"obel



Sun, 12 Sep 2004 16:30:34 GMT  
 Wait function not using any recources
you could use the API Sleep(), that freeze the application for a specific
time (in milliseconds)
the drawback is that also the UI is frozen, so in doesn't respond to user
interaction



Quote:
> I'm searching a function that i can call to wait for
> some seconds and which is not using any CPU-time.



Sun, 12 Sep 2004 17:00:55 GMT  
 Wait function not using any recources
Well - I think that your search is over

A few months ago I was wondering how VB manages to 'halt' the flow of
execution when you display a modal form.

That led me to do a little digging around - and I found something that
I've never seen mentioned on the NGs

the WaitMessage API

What it does is sit waiting for a Windows Message - unlike Sleep that
literally halts the thread - and unlike a DoEvents loop that 'eats'
CPU time.

While Something = False
           WaitMessage
           DoEvents
           ' Me.Print ".";
           ' DoEvents
Wend

Knock out the comment characters and you should see what it does.

Using this I have managed to port a DOS Touchscreen Application to
Windows - it uses something like 50 screens - but only one Form as I
paint all the screens myself.

The nice thing about it is that the code does _not_ have to be a
tangle of Events - all I do is paint a screen and wait until the user
'clicks' on a 'vector' - at which point it comes back with the 'key'
of the thing they clicked - the whole thing runs off the 'strand of
execution' from Sub Main

One could use this method to port DOS Applications that rely on InKey

My hunch is that this could also be used to 'supervise' normal
controls.

In your case you just release the WaitMessage loop using a flag set by
the timer and your code continues where you left off.

On Wed, 27 Mar 2002 09:30:34 +0100, "Goebel, Matthias"

Quote:

>I'm searching a function that i can call to wait for
>some seconds and which is not using any CPU-time.

>A simple loop with the 'doevents' command an checking
>the the system time uses 100% of CPU-Time.
>Now I am using the timer control-element to call a subroutine
>at 1 second interval for example and then counting the elapsed
>time.
>Does anyone know a better/easyer way to do this ?

>Matthias G"obel



Sun, 12 Sep 2004 19:21:02 GMT  
 Wait function not using any recources

Quote:
> What it does is sit waiting for a Windows Message - unlike Sleep that
> literally halts the thread - and unlike a DoEvents loop that 'eats'
> CPU time.

Correct me if I'm wrong, but WaitMessage simply suspends the thread until a
message arrives. These messages, of course, can be from a number of sources
(repaints after another window has partially covered your VB Form, mouse
movement over your Form, stuff like that) and your thread will be suspended
indefinitely until such an event occurs. It would be very useful if you
wanted to wait until a specific message arrived (which you could determine
using the PeekMessage API function), but it wouldn't be much use for waiting
for a *specified* period of time while maintaining your app in a responsive
state (as the original poster seems to want).

Personally, I would stick with the Sleep API for such purposes. If you use a
10 millisecond Sleep period in a loop and your code checks the elapsed time
at each iteration of the loop (and issues a DoEvents) then your app will
remain responsive and your "paused" code will still wait for whatever period
you specify. The time taken for your code to check the elapsed time and to
Do the Events would invariably be very small (almost insignificant when
compared to 10 milliseconds) and so your "paused" code block is effectively
using almost no processor time at all during the total "Sleep". I think the
original poster was using a DoEvents in a loop without using a Sleep in the
same loop, and that is why his app was using a significant amount of
processor time during the "wait".

WaitMessage is, however, extremely useful for some purposes, as you have
suggested.

Mike



Sun, 12 Sep 2004 20:33:13 GMT  
 Wait function not using any recources
On Wed, 27 Mar 2002 12:33:13 -0000, "Maureen"

Hi Mike,

Or should I say 'Maureen' as you have your frock on today.

The Waitmessage waits for a message - but if the OP sets up a Timer
then he should get a message.

Realistically it depends what he wants to do - if he is happy 'dying'
for say 60 secs then Sleep is the answer
- if on the other hand he wants to keep a semblance of 'life'
- well then it is lots of 'little Sleeps' rather than one 'Big Sleep'
- or using Waitmessage.

Regardless - I am so chuffed at having found a way of making Windows
work 'sanely' that I'm keen to see what others come up with.

I have a gut instinct that WaitMessage is a thoroughly subversive
little item, that will allow us to 'break' the chastity belt that MS
has built into VB



Sun, 12 Sep 2004 21:08:41 GMT  
 Wait function not using any recources

Quote:
> On Wed, 27 Mar 2002 12:33:13 -0000, "Maureen"

> Hi Mike,

> Or should I say 'Maureen' as you have your frock on today.

Actually, it's rather a nice frock. A sort of loose and flowing pale blue
silk, covered with little pink and lilac flowers. Maureen's wearing it at
the moment, but I've got plans for it later ;-)

Mike



Sun, 12 Sep 2004 21:27:42 GMT  
 Wait function not using any recources
Well - just don't forget:   Option Explicit

Or maybe a 'DefInt A-Z' will do

On Wed, 27 Mar 2002 13:27:42 -0000, "Maureen"

Quote:



>> On Wed, 27 Mar 2002 12:33:13 -0000, "Maureen"

>> Hi Mike,

>> Or should I say 'Maureen' as you have your frock on today.

>Actually, it's rather a nice frock. A sort of loose and flowing pale blue
>silk, covered with little pink and lilac flowers. Maureen's wearing it at
>the moment, but I've got plans for it later ;-)

>Mike



Sun, 12 Sep 2004 21:54:22 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. Good sites/recources for using Crystal Rpts?

2. Activate COM function and not wait for execution

3. Waiting.......waiting.....waiting....

4. Can VB5.0 view network recources

5. Function marked as restricted or uses a type not supported in Visual Basic

6. problem using component with asp page -- result of function not returned, instead original value returned

7. Function marked as restricted or uses a type not supported in Visual Basic

8. Using FileLen function on Win95 machine not working??

9. Function marked as restricted or uses a type not supported in Visual Basic

10. Wait function

11. Stopping a function, wait on input.

12. LEN() function (was: WAIT statement)

 

 
Powered by phpBB® Forum Software