Various answers suggest that itβs good to sleep inside the stream, for example: Avoid sleep . Why exactly? One of the reasons why it is often given is that it is difficult to gracefully exit the stream (signaling its cessation) if it is sleeping.
Let's say I wanted to periodically check for new files in a network folder, perhaps every 10 seconds. This seems ideal for a stream with a priority set to low (or lowest), because I don't want the potentially lengthy file I / O process to affect my main stream.
What are the alternatives? The code is specified in Delphi, but applies equally to any multi-threaded application:
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;
Minor modification possible:
// Rest a little before spinning around again nSleepCounter := 0; while (not Terminated) and (nSleepCounter < 500) do begin Sleep(TwentyMilliseconds); Inc(nSleepCounter); end;
but it is still related to ...
source share