Delphi Pages Forums  

Go Back   Delphi Pages Forums > Delphi Forum > General

Lost Password?

Reply
 
Thread Tools Display Modes
  #1  
Old 06-23-2002, 08:48 AM
leftover leftover is offline
Junior Member
 
Join Date: May 2002
Posts: 8
Default How to prevent while-task to take all the processor time?

Hi,

How can I prevent processor load to jump 100% when I'm processing while-task with my program. I know, while is for lazy men, but i'm one of them ;-)

So please, if you got any ideas, reply.

LeftOver
Reply With Quote
  #2  
Old 06-23-2002, 08:52 AM
gLes gLes is offline
Senior Member
 
Join Date: Jan 2001
Posts: 2,273
Default RE: How to prevent while-task to take all the processor time?

Well...try putting this line to the beginning/end of the while operation:

Application.ProcessMessages;

This way you will be able to click on buttons and all while the while-loop is in progress...

If it takes all your CPU to do what you do in the while-loop then it's yourfault...don't do such things...

And I must correct you...the while-loop is a very handy and useful programming-technique...on the otherhand Delphi is fully developed for lazy people...

Hope that helps...

gLes

"If you read your bible you might have seen that it was damnation without relief..."
/Rowan Atkinson/
Reply With Quote
  #3  
Old 06-23-2002, 09:04 AM
GoodFun GoodFun is offline
Senior Member
 
Join Date: Jan 2001
Posts: 2,645
Default RE: How to prevent while-task to take all the processor time?

you might want to consider to call Application.ProcessMessages not every time you go through the loop as this will affect the time spent to do the processing quite a bit. To solve that you can for example have a count variable that you increase every time you go through the loop and only every 10 times you call Application.ProcessMessages... something like this:

var LoopCount: Integer;

LoopCount := 0;
while (YourExitCondition) do
begin
// do your processing here


Inc(LoopCount);
// change the 10 below the a number that makes sense...
// usually it's ok if an application reacts about 2 times
// per second... take into account that some people might
// have slower computers
if ((LoopCount mod 10) = 0) then
begin
Application.ProcessMessages;
end;
end;

If you want to be save, you can base the whole thing on time like this:

var LastRefresh: TDateTime;
var Interval: TDateTime; // this is the time between reactions

// adjust this number below to your liking
Interval := EncodeTime(0,0,0,500); // make it half a second
LastRefresh := Now;
while (YourExitCondition) do
begin
// do your processing here


if ((LastRefresh + Interval) < Now) then
begin
Application.ProcessMessages;
LastRefresh := Now;
end;
end;


Hope this helps,

Marcel

Please click on Accept if this helped...
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On

Forum Jump


All times are GMT. The time now is 08:23 PM.


Powered by vBulletin® Version 3.8.8
Copyright ©2000 - 2017, vBulletin Solutions, Inc.