c# – 替代线程内部的睡眠
发布时间:2020-12-15 08:22:48 所属栏目:百科 来源:网络整理
导读:各种答案都表明在一个线程内睡觉是一个坏主意,例如: Avoid sleep.为什么这么做?经常给出的一个原因是,如果正在休眠,很难优雅地退出线程(通过发信号通知它终止). 假设我想定期检查网络文件夹中的新文件,可能每10秒检查一次.这对于优先级设置为低(或最低)的
各种答案都表明在一个线程内睡觉是一个坏主意,例如:
Avoid sleep.为什么这么做?经常给出的一个原因是,如果正在休眠,很难优雅地退出线程(通过发信号通知它终止).
假设我想定期检查网络文件夹中的新文件,可能每10秒检查一次.这对于优先级设置为低(或最低)的线程来说似乎是完美的,因为我不希望可能耗时的文件I / O影响我的主线程. 有哪些替代方案?代码在Delphi中给出,但同样适用于任何多线程应用程序: procedure TNetFilesThrd.Execute(); begin try while (not Terminated) do begin // Check for new files // ... // Rest a little before spinning around again if (not Terminated) then Sleep(TenSeconds); end; finally // Terminated (or exception) so free all resources... end; end; 一个小修改可能是: // Rest a little before spinning around again nSleepCounter := 0; while (not Terminated) and (nSleepCounter < 500) do begin Sleep(TwentyMilliseconds); Inc(nSleepCounter); end; 但这仍然涉及睡眠…… 解决方法
执行此操作的标准方法是等待取消事件.在伪代码中看起来像这样:
while not Terminated do begin // Check for new files // ... // Rest a little before spinning around again FTerminationEvent.WaitFor(TenSeconds); end; 要终止,您将覆盖TerminatedSet: procedure TMyThread.TerminatedSet; begin inherited; FTerminationEvent.SetEvent; // abandon the wait in the thread method end; 事件的等待时间超时,或因事件发出信号而终止.这允许您的线程暂停一段时间而不会给CPU带来负担,同时还能保持对终止请求的响应. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |