var CurrTime: TSystemTime; Sec: Word; begin GetSystemTime(CurrTime); Sec := CurrTime.wSecond + 1; while Sec>CurrTime.wSecond do GetSystemTime(CurrTime); end;
constructor Create(EventAttributes: PSecurityAttributes; ManualReset, InitialState: Boolean; const Name: string);DWORD WaitForSingleObject( HANDLE hHandle, // handle to object to wait for DWORD dwMilliseconds // time-out interval in milliseconds evtTmp : Tevent; evtTmp := Tevent.creat(nil,true,false,'testreasdfsad')//事件 WaitForSingleObject(evtTmp,1000); //等待1000毫秒。(此时进程或线程挂起1000毫秒) );
看看 msdn 关于 sleep。 public static native void sleep( long millis ) Causes the currently executing thread to sleep (temporarily cease execution) for the specified number of milliseconds. The thread does not lose ownership of any monitors 当线程执行sleep(1000),线程并没有交出所有权,所以于事无补。每当我看到 sleep函数的时候,我总会想起dos编程时代,那都是5,6年前的事了。很快是吗? 都过时了,sleep也一样。我从不用sleep,你还不如用 替换WaitForSingleObject(hVar, 1000);因为他才真正挂起, 交出cpu控制权。 好好看 WaitForSingleObject(hVar, 1000);等相关的信号量控制函数,那是windows 多线程的精髓。
Sleep好像并不会影响其他线程。 我觉得简单就好,就算浪费一点点CPU又有什么要紧。
delphi6_fan(英博思)的思想应该基本正确吧
这个问题的答案应该就是 Sleep。这是最简单、安全、无副作用、完全满足题目要求的答案。 如果是在主线程中使用,可以参考delphi6_fan(英博思)的方法,但要注意事件处理代码的重入问题。 jacksoft(小半)的方法跟 Sleep 比起来只有麻烦,没有任何优点。airhorse(编程至尊宝): “当线程执行sleep(1000),线程并没有交出所有权,所以于事无补。” 题目并无要求“线程交出所有权”,延时的目的已达到,如何“于事无补” ? 我对 windows 底层了解不多,从“The thread does not lose ownership of any monitors”这句话本身也说明不了什么,我不明白为什么要扯到“线程交出所有权”。我只知道,Sleep 在达到延时目的的同时,即使不是完全不占用 CPU,也是微乎其微。另外,这段文字是在 MFC 中,是否与Sleep API等同我这只菜鸟也不知道。不管如何,这都与题目要求无关,你也太小题大做了吧。想把初学者吓跑是不是?
to ImFool(搞技术的都是傻瓜) : sleep,是問題多多的東東,爲什麽有那麽多人用,那是因爲那有一個好名字.通俗易懂,童叟無欺.
CurrTime: TSystemTime;
Sec: Word;
begin
GetSystemTime(CurrTime);
Sec := CurrTime.wSecond + 1;
while Sec>CurrTime.wSecond do
GetSystemTime(CurrTime);
end;
var t1,t2:Integer;
begin
t1:=GetTickCount;
t2:=t1;
while t2-t1<ATime do
t2:=GetTickCount;
end;
//------------------------------------------------------------------------------//(1) 功 能 : 延迟
//(2) 参 数 : msecs-延迟毫秒数
//(3) 返回值 : 无
//(4) 作成者 : delphi6_fan
//------------------------------------------------------------------------------var
FirstTickCount:longint;
begin
FirstTickCount:=GetTickCount;
repeat
Application.ProcessMessages;//此句关键,保证其它线程不受影响
until ((longint(GetTickCount)-longint(FirstTickCount)) >= longint(msecs));
end;
constructor Create(EventAttributes: PSecurityAttributes; ManualReset, InitialState: Boolean; const Name: string);DWORD WaitForSingleObject(
HANDLE hHandle, // handle to object to wait for
DWORD dwMilliseconds // time-out interval in milliseconds evtTmp : Tevent;
evtTmp := Tevent.creat(nil,true,false,'testreasdfsad')//事件
WaitForSingleObject(evtTmp,1000); //等待1000毫秒。(此时进程或线程挂起1000毫秒)
);
public static native void sleep( long millis ) Causes the currently executing thread to sleep
(temporarily cease execution) for the specified number of milliseconds.
The thread does not lose ownership of any monitors 当线程执行sleep(1000),线程并没有交出所有权,所以于事无补。每当我看到 sleep函数的时候,我总会想起dos编程时代,那都是5,6年前的事了。很快是吗?
都过时了,sleep也一样。我从不用sleep,你还不如用 替换WaitForSingleObject(hVar, 1000);因为他才真正挂起,
交出cpu控制权。 好好看 WaitForSingleObject(hVar, 1000);等相关的信号量控制函数,那是windows 多线程的精髓。
我觉得简单就好,就算浪费一点点CPU又有什么要紧。
如果是在主线程中使用,可以参考delphi6_fan(英博思)的方法,但要注意事件处理代码的重入问题。
jacksoft(小半)的方法跟 Sleep 比起来只有麻烦,没有任何优点。airhorse(编程至尊宝):
“当线程执行sleep(1000),线程并没有交出所有权,所以于事无补。”
题目并无要求“线程交出所有权”,延时的目的已达到,如何“于事无补” ?
我对 windows 底层了解不多,从“The thread does not lose ownership of any monitors”这句话本身也说明不了什么,我不明白为什么要扯到“线程交出所有权”。我只知道,Sleep 在达到延时目的的同时,即使不是完全不占用 CPU,也是微乎其微。另外,这段文字是在 MFC 中,是否与Sleep API等同我这只菜鸟也不知道。不管如何,这都与题目要求无关,你也太小题大做了吧。想把初学者吓跑是不是?
sleep,是問題多多的東東,爲什麽有那麽多人用,那是因爲那有一個好名字.通俗易懂,童叟無欺.
你說呢?
我没发现 Sleep 有任何问题。
让我见识一下好吗?真的,我是菜鸟一只而已。