首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > .NET >

Delphi不占资源的等待方法,该怎么处理

2012-03-19 
Delphi不占资源的等待方法sleep会让程序被挂起,所以自己写个等待函数:timestart:GetTickCount()timestop

Delphi不占资源的等待方法
sleep会让程序被挂起,所以自己写个等待函数:
    timestart:=GetTickCount();
    timestop:=timestart;
    While   (timestop-timestart)   <x   do
    begin
    timestop:=GetTickCount();
    end;

如果启动一个程序的话,还可以正常使用。但是如果我启动3~4个程序,那么CPU的使用率就会一直保持100%,系统什么都做不了。
有没有什么办法让等待的时候程序不占用资源或者占用很少?

[解决办法]
timestart:=GetTickCount();
timestop:=timestart;
While (timestop-timestart) <x do
begin
timestop:=GetTickCount();
Application.ProcessMessages;
end;
[解决办法]
在循环里加上Application.ProcessMessages;
[解决办法]
Application.ProcessMessages
用这个有时候也是很耗费资源的 我是真实经历过一次 所以我现在是用下面的方法:
Procedure Delay(nValue:integer);
var
t:integer;
tmpMSG:TMsg;
begin
t:=Gettickcount();
repeat
FreeCPU();
until Gettickcount()-t> nValue;
end;
procedure FreeCPU();
var
tmpMSG:TMsg;
begin
GetMessage(tmpMSG,Thandle(nil),0,0);
TranslateMessage(tmpMSG);
DispatchMessage(tmpMSG);
end;

这样效果似乎比直接用Application.ProcessMessages稍微好些 你可以试试
[解决办法]
procedure Delay(msec: single);
//延时函数,msec 为微秒(千分之1秒)
var
FirstTickCount: real;
begin
if msec > 0 then
begin
FirstTickCount := GetTickCount();
FirstTickCount := FirstTickCount + msec;
while FirstTickCount > GetTickCount() do
Application.HandleMessage; //关键在这里
end;
end;
//保证完成你的需要,别忘了多给点分。

热点排行