| View previous topic :: View next topic |
| Author |
Message |
Bear Guest
|
Posted: Fri Feb 27, 2004 4:30 am Post subject: How to make the cpu usage ratio lower in while loop ?! |
|
|
CPU usage : 100%
===================
procedure TfrmMain.Button1Click(Sender: TObject);
var I : Integer;
begin
I:=-1;
while (I<0) do
begin
Application.ProcessMessages;
end;
end;
CPU usage : about 10% , normal
===================
procedure TfrmMain.Button1Click(Sender: TObject);
var I : Integer;
begin
I:=-1;
while (I<0) do
begin
Sleep(1); // <-- Look here !!
Application.ProcessMessages;
end;
end;
--
_/_/_/_/ _/_/_/_/ _/_/ _/_/_/
_/ _/ _/ _/ _/ _/ _/
_/_/_/ _/_/_/ _/_/ _/ _/_/_/
_/ _/ _/ _/ _/ _/ _/
_/_/_/_/ _/_/_/_/ _/ _/ _/ _/
founder of http://www.delphidevelopers.com
|
|
| Back to top |
|
 |
Chad Z. Hower aka Kudzu Guest
|
Posted: Fri Feb 27, 2004 5:24 am Post subject: Re: How to make the cpu usage ratio lower in while loop ?! |
|
|
"Bear" <dogbear (AT) 163 (DOT) net> wrote in news:403ec808 (AT) newsgroups (DOT) borland.com:
| Quote: | CPU usage : about 10% , normal
===================
procedure TfrmMain.Button1Click(Sender: TObject);
var I : Integer;
begin
I:=-1;
while (I<0) do
begin
Sleep(1); // <-- Look here !!
Application.ProcessMessages;
end;
end;
|
And your loop will run slower. This is like reducing fuel consumption in a
car by disconnecting spark plugs.
--
Chad Z. Hower (a.k.a. Kudzu) - http://www.hower.org/Kudzu/
"Programming is an art form that fights back"
Got Indy? Got the book?
http://www.atozed.com/indy/book/
|
|
| Back to top |
|
 |
Remy Lebeau (TeamB) Guest
|
Posted: Fri Feb 27, 2004 8:10 am Post subject: Re: How to make the cpu usage ratio lower in while loop ?! |
|
|
"Bear" <dogbear (AT) 163 (DOT) net> wrote
| Quote: | How to make the cpu usage ratio lower in while loop ?!
|
You already answered your own question. Use Sleep() to make the while loop
relinquish the calling thread's remaining timeslice to other threads in the
system that are still waiting to execute.
Otherwise, don't call ProcessMessages() so often to begin with.
Gambit
|
|
| Back to top |
|
 |
Martin James Guest
|
Posted: Fri Feb 27, 2004 3:21 pm Post subject: Re: How to make the cpu usage ratio lower in while loop ?! |
|
|
Get rid of the application.processMessages altogether by using threads
and.or state machines:
Idle CPU usage 0%
User input Latency:minimum.
Rentrancy protection: not required.
Rgds,
Martin
|
|
| Back to top |
|
 |
Jack Mays Guest
|
Posted: Fri Feb 27, 2004 9:19 pm Post subject: Re: How to make the cpu usage ratio lower in while loop ?! |
|
|
"Martin James" <mjames_falcon (AT) dial (DOT) pipex.com> wrote
| Quote: | Get rid of the application.processMessages altogether by using threads
and.or state machines:
Idle CPU usage 0%
|
Perhaps you ment "CPU usage 0%"..
Idle CPU suage of 0% would mean something is eating all the processor <g>
--
Jack Mays
|
|
| Back to top |
|
 |
Martin James Guest
|
Posted: Fri Feb 27, 2004 11:02 pm Post subject: Re: How to make the cpu usage ratio lower in while loop ?! |
|
|
| Quote: |
Idle CPU usage 0%
Perhaps you ment "CPU usage 0%"..
Idle CPU suage of 0% would mean something is eating all the processor
|
CPU suage I recognise as a term for W95, but misspelled :)
Rgds,
Martin
|
|
| Back to top |
|
 |
Bear Guest
|
Posted: Sat Feb 28, 2004 2:14 am Post subject: Problem Background : WaitForReady(WebBrowser); Re: How to |
|
|
the background of the CPU usage test is that I am writing a function
WaitForReady(WebBrowser);
the function WaitForReady has been tested and used in my projects !
in my project i must do some process after webbrowser load document but
OnCocumentComplete event is not suitable for me.
Source Code :
========================================================
function WaitForReady(WebBrowser:TWebBrowser) : Boolean;
const C_MaxWaitSeconds = 40; // 40 Seconds
C_BeginCheckTimes = 200; //Time check after 200 times loops
C_SleepMiniSecondsEachTime = 20 ;// for Sleep(20)
var BeginTime : TDateTime;
RepeatTimes : Integer;
begin
{ if you use Sleep derectly after WebBrowser.Navigae, WebBrowser will be
blocked }
Application.ProcessMessages;
RepeatTimes := 0;
BeginTime := Now;
repeat
Application.ProcessMessages;
{ avoid 60,000--150,000 times of loops ,
also you can use Sleep(2000) ,
it will not slow down the speed of WebBrowser !!
but it will slow down the CPU usage from 100% to about 10-20%
}
Sleep(C_SleepMiniSecondsEachTime);
Inc(RepeatTimes);
if RepeatTimes > C_BeginCheckTimes then
begin
if SecondsBetween(Now,BeginTime)> C_MaxWaitSeconds then
begin
Result := False;
WebBrowser.Stop;
Application.ProcessMessages;
Exit;
end;
end;
{
Don't use WebBrowser.Busy !
WebBrowser.Busy will be false if your access it after calling
WebBrowser.Navigate at once
}
until (WebBrowser.ReadyState = READYSTATE_COMPLETE);
Result := True;
Application.ProcessMessages;
end;
========================================================
Bear
http://www.delphidevelopers.com
"Bear" <dogbear (AT) 163 (DOT) net> 写入邮件 news:403ec808 (AT) newsgroups (DOT) borland.com...
| Quote: | CPU usage : 100%
===================
procedure TfrmMain.Button1Click(Sender: TObject);
var I : Integer;
begin
I:=-1;
while (I<0) do
begin
Application.ProcessMessages;
end;
end;
CPU usage : about 10% , normal
===================
procedure TfrmMain.Button1Click(Sender: TObject);
var I : Integer;
begin
I:=-1;
while (I<0) do
begin
Sleep(1); // <-- Look here !!
Application.ProcessMessages;
end;
end;
--
_/_/_/_/ _/_/_/_/ _/_/ _/_/_/
_/ _/ _/ _/ _/ _/ _/
_/_/_/ _/_/_/ _/_/ _/ _/_/_/
_/ _/ _/ _/ _/ _/ _/
_/_/_/_/ _/_/_/_/ _/ _/ _/ _/
founder of http://www.delphidevelopers.com
|
|
|
| Back to top |
|
 |
Bear Guest
|
Posted: Sat Feb 28, 2004 2:17 am Post subject: Re: Problem Background : WaitForReady(WebBrowser); Re: How |
|
|
Sorry, "OnCocumentComplete" event shoud be "OnDocumentComplete event"
Bear
"Bear" <dogbear (AT) 163 (DOT) net> 写入邮件 news:403ff99d (AT) newsgroups (DOT) borland.com...
| Quote: | the background of the CPU usage test is that I am writing a function
WaitForReady(WebBrowser);
the function WaitForReady has been tested and used in my projects !
in my project i must do some process after webbrowser load document but
OnCocumentComplete event is not suitable for me.
Source Code :
========================================================
function WaitForReady(WebBrowser:TWebBrowser) : Boolean;
const C_MaxWaitSeconds = 40; // 40 Seconds
C_BeginCheckTimes = 200; //Time check after 200 times loops
C_SleepMiniSecondsEachTime = 20 ;// for Sleep(20)
var BeginTime : TDateTime;
RepeatTimes : Integer;
begin
{ if you use Sleep derectly after WebBrowser.Navigae, WebBrowser will be
blocked }
Application.ProcessMessages;
RepeatTimes := 0;
BeginTime := Now;
repeat
Application.ProcessMessages;
{ avoid 60,000--150,000 times of loops ,
also you can use Sleep(2000) ,
it will not slow down the speed of WebBrowser !!
but it will slow down the CPU usage from 100% to about 10-20%
}
Sleep(C_SleepMiniSecondsEachTime);
Inc(RepeatTimes);
if RepeatTimes > C_BeginCheckTimes then
begin
if SecondsBetween(Now,BeginTime)> C_MaxWaitSeconds then
begin
Result := False;
WebBrowser.Stop;
Application.ProcessMessages;
Exit;
end;
end;
{
Don't use WebBrowser.Busy !
WebBrowser.Busy will be false if your access it after calling
WebBrowser.Navigate at once
}
until (WebBrowser.ReadyState = READYSTATE_COMPLETE);
Result := True;
Application.ProcessMessages;
end;
========================================================
Bear
http://www.delphidevelopers.com
"Bear" <dogbear (AT) 163 (DOT) net> 写入邮件 news:403ec808 (AT) newsgroups (DOT) borland.com...
CPU usage : 100%
===================
procedure TfrmMain.Button1Click(Sender: TObject);
var I : Integer;
begin
I:=-1;
while (I<0) do
begin
Application.ProcessMessages;
end;
end;
CPU usage : about 10% , normal
===================
procedure TfrmMain.Button1Click(Sender: TObject);
var I : Integer;
begin
I:=-1;
while (I<0) do
begin
Sleep(1); // <-- Look here !!
Application.ProcessMessages;
end;
end;
--
_/_/_/_/ _/_/_/_/ _/_/ _/_/_/
_/ _/ _/ _/ _/ _/ _/
_/_/_/ _/_/_/ _/_/ _/ _/_/_/
_/ _/ _/ _/ _/ _/ _/
_/_/_/_/ _/_/_/_/ _/ _/ _/ _/
founder of http://www.delphidevelopers.com
|
|
|
| Back to top |
|
 |
Bear Guest
|
Posted: Sat Feb 28, 2004 2:23 am Post subject: Re: How to make the cpu usage ratio lower in while loop ?! |
|
|
| Quote: | And your loop will run slower. This is like reducing fuel consumption in a
car by disconnecting spark plugs.
|
Yeah, but i use it for wait webbrowser ready.
hi, Chad, did you see the Chinese version of your article "why delphi.net"
?
http://www.delphidevelopers.com/borland/why_delphi_dotnet.htm
Bear
www.delphidevelopers.com
|
|
| Back to top |
|
 |
Remy Lebeau (TeamB) Guest
|
Posted: Sat Feb 28, 2004 4:36 am Post subject: Re: How to make the cpu usage ratio lower in while loop ?! |
|
|
"Bear" <dogbear (AT) 163 (DOT) net> wrote
| Quote: | Yeah, but i use it for wait webbrowser ready.
|
Then you should have shown as much to begin with. There are other ways to
handle waiting for the browser, though.
Gambit
|
|
| Back to top |
|
 |
Bear Guest
|
Posted: Sat Feb 28, 2004 7:19 am Post subject: Re: How to make the cpu usage ratio lower in while loop ?! |
|
|
"Remy Lebeau (TeamB)" <gambit47.no.spam (AT) no (DOT) spam.yahoo.com> 写入邮件
news:404018e1 (AT) newsgroups (DOT) borland.com...
| Quote: |
Yeah, but i use it for wait webbrowser ready.
Then you should have shown as much to begin with. There are other ways to
handle waiting for the browser, though.
|
Would you like to give me several examples ? (my source code of WaitForReady
function has been post at the end thread)
Bear
www.delphidevelopers.com
|
|
| Back to top |
|
 |
Chad Z. Hower aka Kudzu Guest
|
Posted: Sun Feb 29, 2004 9:11 pm Post subject: Re: How to make the cpu usage ratio lower in while loop ?! |
|
|
"Bear" <dogbear (AT) 163 (DOT) net> wrote in news:403ffbad (AT) newsgroups (DOT) borland.com:
Yes. I took a look right after you transalted it. :)
Have you had many hits?
--
Chad Z. Hower (a.k.a. Kudzu) - http://www.hower.org/Kudzu/
"Programming is an art form that fights back"
Qualified help FAST with Indy Experts Support
from the experts themselves:
http://www.atozed.com/indy/experts/support.iwp
|
|
| Back to top |
|
 |
Bear Guest
|
Posted: Tue Mar 02, 2004 12:22 pm Post subject: a lot ! Re:Have you had many hits? |
|
|
A lot of hits.
I do not stat the hits on my site but i post it on my column on CSDN (China
Software Develope Network). more than 14000 hits. and a lot of comments.
Look here :http://www.csdn.net/develop/read_article.asp?id=21101
Bear
www.delphidevelopers.com
"Chad Z. Hower aka Kudzu" <cpub (AT) hower (DOT) org> 写入邮件
news:Xns949EEBF6444FCcpub (AT) 127 (DOT) 0.0.1...
|
|
| Back to top |
|
 |
Chad Z. Hower aka Kudzu Guest
|
Posted: Tue Mar 02, 2004 7:51 pm Post subject: Re: a lot ! Re:Have you had many hits? |
|
|
"Bear" <dogbear (AT) 163 (DOT) net> wrote in news:40447ca4$2 (AT) newsgroups (DOT) borland.com:
| Quote: | A lot of hits.
I do not stat the hits on my site but i post it on my column on CSDN (China
Software Develope Network). more than 14000 hits. and a lot of comments.
|
Wow!
I can only read one of the comments. I hope the rest are good as well. :)
--
Chad Z. Hower (a.k.a. Kudzu) - http://www.hower.org/Kudzu/
"Programming is an art form that fights back"
Need extra help with an Indy problem?
http://www.atozed.com/indy/experts/
|
|
| Back to top |
|
 |
Bear Guest
|
Posted: Wed Mar 03, 2004 12:54 am Post subject: Re: a lot ! Re:Have you had many hits? |
|
|
"Chad Z. Hower aka Kudzu" <cpub (AT) hower (DOT) org> wrote:
| Quote: | I can only read one of the comments. I hope the rest are good as well.
|
Most of the rest comments express that they support Delphi and Borland ,
and like to see more articles like this one.
there are a lot of delphi developers in China.
Bear
www.delphidevelopers.com
|
|
| Back to top |
|
 |
|