2楼的朋友,我不知道你明白你所贴的代码的含义没? CreateEvent函数一般在多线程中使用,你在这里创建事件有啥意思?个人认为你的代码十分多余,我贴一个比较短小的过程实现楼主的需求:procedure pause(t:cardinal); begin t:=t+GetTickCount; while t>GetTickCount do Application.ProcessMessages; end;
不行不行 CPU 到50了! 可有其他方法?
不行,也到底哪一种方法不行啊没有sleep(10)的循环,cpu是会高的
function SP(MaxWaitTime: Cardinal): Boolean; var I:Integer;WaitedTime:Cardinal; begin WaitedTime:=0; while (WaitedTime<MaxWaitTime) do begin SleepEx(100,False); Inc(WaitedTime,100); Application.ProcessMessages ; end end;调用:SP(3000);
2楼的朋友,我没说你的代码不能用,我只是说有些冗余。这个冗余就相当于是你有定义了100个变量而不去用一样,虽然功能实现,但完全多余。如果楼主认为cpu占用还高,可再做如下调整: procedure pause(t:cardinal); begin t:=t+GetTickCount; while t>GetTickCount do begin Sleep(100);Application.ProcessMessages;end; end关于楼husion01所说的“点右上角关闭程序时关不了”的问题,其实加一行代码即可避免这个问题。但因为此问题不是楼主提出来的,而且楼主只给了20分,所以暂时就不关注了。
sleep(10);
application.processmessages;直到够时间
//让循环延时一段时间的函数
procedure Delay(msecs:integer);
var
Tick: DWord;
Event: THandle;
begin
Event := CreateEvent(nil, False, False, nil);
try
Tick := GetTickCount + DWord(msecs);
while (msecs > 0) and (MsgWaitForMultipleObjects(1, Event, False, msecs, QS_ALLINPUT) <> WAIT_TIMEOUT) do
begin Application.ProcessMessages; msecs := Tick - GetTickcount; end;
finally CloseHandle(Event); end;
end;
也是网上下的,经过本人测试,非常好用。
再用个线程给个提示,告知用户 程序还在运行!
在主线程中sleep的话就应该不是假死了吧...停止一切活动,装死1分钟!哈哈
sleep(10);
application.processmessages;直到够时间
CreateEvent函数一般在多线程中使用,你在这里创建事件有啥意思?个人认为你的代码十分多余,我贴一个比较短小的过程实现楼主的需求:procedure pause(t:cardinal);
begin
t:=t+GetTickCount;
while t>GetTickCount do Application.ProcessMessages;
end;
可有其他方法?
var I:Integer;WaitedTime:Cardinal;
begin
WaitedTime:=0;
while (WaitedTime<MaxWaitTime) do
begin
SleepEx(100,False);
Inc(WaitedTime,100);
Application.ProcessMessages ;
end
end;调用:SP(3000);
procedure pause(t:cardinal);
begin
t:=t+GetTickCount;
while t>GetTickCount do begin Sleep(100);Application.ProcessMessages;end;
end关于楼husion01所说的“点右上角关闭程序时关不了”的问题,其实加一行代码即可避免这个问题。但因为此问题不是楼主提出来的,而且楼主只给了20分,所以暂时就不关注了。