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

等待函数,该怎么解决

2012-02-06 
等待函数procedure mywait(x:cardinal) Stdcallvartimestart,timestop:DWORDbegintimestart:GetTickCo

等待函数
procedure mywait(x:cardinal); Stdcall;  
 var
 timestart,timestop:DWORD;
begin
  timestart:=GetTickCount();
  timestop:=timestart;
  While (timestop-timestart) <x do
  begin
  timestop:=GetTickCount();
  Application.ProcessMessages;
  end;
end;

上面这个是写的等待函数,如果直接在程序里使用mywait,CPU使用率会100%,如果用sleep程序会被挂起
如果在线程里使用mywait,CPU使用率也会100%,如果用sleep的话CPU使用率也会在50%左右

delphi还有更节省资源的等待方式了吗?

[解决办法]
procedure TForm1.Button1Click(Sender: TObject);
begin
self.Timer1.Interval:=5000;//等待5秒
self.Timer1.Enabled:=true;
end;

procedure TForm1.Timer1Timer(Sender: TObject);
begin
self.Timer1.Enabled:=false;
showmessage('5秒到了');
end;
[解决办法]
这样就可以多少调用了
procedure TForm1.Wait(seconds:integer;event:TNOtifyEvent); 
begin 
self.Timer1.Interval:=seconds*1000;
self.Timer1.Enabled:=true;
self.timer1.ontimer:=event;
end; 



[解决办法]
procedure mywait(x:cardinal); Stdcall;
var
 timestart,timestop:DWORD;
begin
timestart:=GetTickCount();
timestop:=timestart;
While (timestop-timestart)<x do
begin
timestop:=GetTickCount();
sleep(10);
Application.ProcessMessages;
end;
end;

热点排行