Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

50%CPU for DO WHIL lb_sel=0 1

Status
Not open for further replies.

lrice

Programmer
Feb 19, 2007
11
US
This little snipet of code takes 50% CPU


* **************** PROBLEM HERE, if walk away for hour, it locks screen ***
* and this must be where cpu is being used so much DO WHILE

lb_sel=0
Do While lb_sel=0
lb_sel=Inkey()
Enddo

anyone know a fix?


 
Yes, do not use loops like this or put some wait state in such loops:
Code:
lb_sel=0
Do While lb_sel=0
   lb_sel=Inkey()
   DOEVENTS
Enddo

Borislav Borissov
VFP9 SP1, SQL Server 2000/2005.
MVP VFP
 
Borislav, thank you. Would there not be a way to slow down the sampling of Inkey() itself?

I ran your code, being unfamiliar with DOEVENTS as a newcomer to vfp7 and it simply oscillated between 47-50% cpu

Before I give up on this loop, I'll search for a way to diminish the sampling rate for keyboard entry.
Maybe vfp has such a command, or maybe it's available in C

But without such a functioning structure, my light bar menu and file browse won't work.





Larry A Rice
 
The READ command doesn't hang up cpu
so it seems reasonable to expect a way for INKEY() to be used without taking over the cpu

But I do realize we could sometimes see this effect in DOS
with such loops

Oh well, I have a great little lightbar routine but it doesn't seem feasible to use it. Now I know why I see so many hanging hour-glasses on the net. :)

I'm gonna find a way to make this program work. Even if I have to drastically change it, the effect is what I want.
I want my own versatile light bar menu. Anybody got one?

Thanks for your time.




Larry A Rice
 
You could use only:

lb_sel = Inkey(0)

That way the program stops and wait until the user press a key (any key). Check INKEY() function in HELP for more info.

Borislav Borissov
VFP9 SP1, SQL Server 2000/2005.
MVP VFP
 
I often use sleep to throttle do while code:

DECLARE Sleep IN Win32API INTEGER

do while .t.
sleep(1000)
other code
enddo
 
Borislav, thank you!!! I should spend more time on the fine print. I shall endeavor to emulate you. Borislav Borissov has told me what was in the books but I didn't take time to experiment with and try. Like my son says, the details, dad,
the details!

I do not know how it happened, but first couple times I tried a variation like that, the thing still hung with 50%

lb_sel=INKEY(10) works just fine..... jumps from nearly 0% to 4%, but that's progress. And extremely helpful.


BORISLAV, thank you for taking the time to tell me this.
And yes, I substituted that into the whole program and it works without eating cpu time, and the whole program still works.

What can I do to thank you, Borislav?

Larry A Rice
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top