What is a better method to suspend program execution until a condition is met?

Fabio Vitale

I need to wait until a mapped network folder (\HostName\NetworkPath) become empty. What I mean is that program flow cannot continue until that network folder is empty.

So far I have the following logic in place but I noticed that it takes time before FindFirst notices that the network folder become empty.

If I keep observing an opened explorer windows, pointing to that network folder, I notice that it become empty far before FindFirst notices it.

I used Sleep(5000) to introduce some delay in calling again CheckNetworkFolderIsEmpty in my while loop, otherwise it is being called too often. But maybe that folder will become empty far before 5 seconds, so 5 seconds is an arbitrary time delay that may results in an unnecessary dealy in program execution, in the event that the folder become empty before.

What can be the culprit, what can be a better alternative?

Also I do not know what else to use instead of a simple Sleep.

while not CheckRawFolderIsEmpty do begin
    Sleep(5000);
end;
function TForm1.CheckNetworkFolderIsEmpty: Boolean;
begin
    Result := (CountFilesInFolder('\\HostName\NetworkPath', '*.txt') = 0);
end;

function CountFilesInFolder(const aPath, aFileMask: string): Integer;
var
    Path: string;
    SearchRec: TSearchRec;
begin
    Path := IncludeTrailingPathDelimiter(aPath);
    Result := 0;
    if FindFirst(Path + aFileMask, faAnyFile and not faDirectory, SearchRec) = 0 then begin
        repeat
            Inc(Result);
        until FindNext(SearchRec) <> 0;
        FindClose(SearchRec);
    end;
end;
Peter Wolf

Observing file system changes like you do is inefficient (FindFirst, FindNext) and inacurate as you've learned. Windows provides API FindFirstChangeNotification for that purpose as J... has pointed out in the comment under your question.

Good news is that you don't need to start studying the API from scratch, because some other people did the hard work for you. Check out some freeware wrappers for Delphi around the API:

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Rxjs observable wait until some condition is met

分類Dev

How do I make a repeating 'if' statement until a condition is met?

分類Dev

python recursion (why won't program exit once if condition is met?)

分類Dev

Can I write a if/else statement for Axios Get Method if condition is not met?

分類Dev

Waiting for a program to load DLLs and then suspend it

分類Dev

How can i call a certain method from the class decorator when some `*ngIf` condition is met?

分類Dev

What is the difference between Hibernate and Suspend

分類Dev

Fill a column in a dataframe if a condition is met

分類Dev

Mysql select by condition unless another condition is met

分類Dev

How to resample until a specific date criteria is met

分類Dev

What do we mean when we say that control of the operating system is passed on to the main() function while execution of a program?

分類Dev

retry until condition not satisfied on karatedsl

分類Dev

Continue iteration with changed parameters after met condition

分類Dev

Pandas Dataframe find first occurence if condition met

分類Dev

Only use $in operator if a certain condition is met - Mongoose

分類Dev

How to unsubscribe immediately after the the condition is met in RxJs

分類Dev

'for' loop terminating before termination condition is met

分類Dev

How to skip iteration in for loop if condition is met

分類Dev

MongoDB: apply $filter stage only if a condition is met?

分類Dev

Update row value with column name if condition is met

分類Dev

R: How to stop a for statement when a if condition is met

分類Dev

Drop all group rows when met a condition?

分類Dev

Removing word from string if condition is met?

分類Dev

how do I better setup the dice program to get a better output

分類Dev

take input until the condition is completed in python

分類Dev

Exit condition not triggering in Do Until loop

分類Dev

print the ratio of Fibonacci numbers until a specific condition

分類Dev

cygwin: output freezes until program is finished

分類Dev

how create a coroutine inside a Controller method in order to call a suspend function

Related 関連記事

  1. 1

    Rxjs observable wait until some condition is met

  2. 2

    How do I make a repeating 'if' statement until a condition is met?

  3. 3

    python recursion (why won't program exit once if condition is met?)

  4. 4

    Can I write a if/else statement for Axios Get Method if condition is not met?

  5. 5

    Waiting for a program to load DLLs and then suspend it

  6. 6

    How can i call a certain method from the class decorator when some `*ngIf` condition is met?

  7. 7

    What is the difference between Hibernate and Suspend

  8. 8

    Fill a column in a dataframe if a condition is met

  9. 9

    Mysql select by condition unless another condition is met

  10. 10

    How to resample until a specific date criteria is met

  11. 11

    What do we mean when we say that control of the operating system is passed on to the main() function while execution of a program?

  12. 12

    retry until condition not satisfied on karatedsl

  13. 13

    Continue iteration with changed parameters after met condition

  14. 14

    Pandas Dataframe find first occurence if condition met

  15. 15

    Only use $in operator if a certain condition is met - Mongoose

  16. 16

    How to unsubscribe immediately after the the condition is met in RxJs

  17. 17

    'for' loop terminating before termination condition is met

  18. 18

    How to skip iteration in for loop if condition is met

  19. 19

    MongoDB: apply $filter stage only if a condition is met?

  20. 20

    Update row value with column name if condition is met

  21. 21

    R: How to stop a for statement when a if condition is met

  22. 22

    Drop all group rows when met a condition?

  23. 23

    Removing word from string if condition is met?

  24. 24

    how do I better setup the dice program to get a better output

  25. 25

    take input until the condition is completed in python

  26. 26

    Exit condition not triggering in Do Until loop

  27. 27

    print the ratio of Fibonacci numbers until a specific condition

  28. 28

    cygwin: output freezes until program is finished

  29. 29

    how create a coroutine inside a Controller method in order to call a suspend function

ホットタグ

アーカイブ