Articles   Members Online:
-Article/Tip Search
-News Group Search over 21 Million news group articles.
-Delphi/Pascal
-CBuilder/C++
-C#Builder/C#
-JBuilder/Java
-Kylix
Member Area
-Home
-Account Center
-Top 10 NEW!!
-Submit Article/Tip
-Forums Upgraded!!
-My Articles
-Edit Information
-Login/Logout
-Become a Member
-Why sign up!
-Newsletter
-Chat Online!
-Indexes NEW!!
Employment
-Build your resume
-Find a job
-Post a job
-Resume Search
Contacts
-Contacts
-Feedbacks
-Link to us
-Privacy/Disclaimer
Embarcadero
Visit Embarcadero
Embarcadero Community
JEDI
Links
How to wait for a file to be created Turn on/off line numbers in source code. Switch to Orginial background IDE or DSP color Comment or reply to this aritlce/tip for discussion. Bookmark this article to my favorite article(s). Print this article
20-Oct-02
Category
Files Operation
Language
Delphi 2.x
Views
66
User Rating
No Votes
# Votes
0
Replies
0
Publisher:
DSP, Administrator
Reference URL:
DKB
			Author: Jonas Bilinkevicius

Does anyone have code that will wait for a file to be created? In particular I'm 
trying to come up with code that has pretty much a 0% processor usage.

Answer:

The following function consumes very litte CPU while waiting for a file to be 
created:
1   
2   function WaitForFile(FileName: string): Boolean;
3   {Wait for a file to be created. Tracks the directory were the file will be created.
4   Returns true if file exists, false on error.}
5   var
6     WaitHandle: THandle;
7   begin
8     Result := False; {Let's assume we failed}
9     WaitHandle := FindFirstChangeNotification(PChar(ExtractFilePath(FileName)),
10      False, FILE_NOTIFY_CHANGE_FILE_NAME);
11    if (INVALID_HANDLE_VALUE = WaitHandle) then
12    begin
13      {The path to the file does not exists}
14      Exit;
15    end;
16    repeat
17      if WaitForSingleObject(WaitHandle, INFINITE) = WAIT_OBJECT_0 then
18      begin {Something happenned in the directory}
19        if FileExists(FileName) then
20        begin
21          result := True;
22          Break; {My file has been created, exit}
23        end;
24        {My file is not there, keep on}
25        if not FindNextChangeNotification(WaitHandle) then
26        begin
27          {Something happened to the directory, maybe it was deleted}
28          Break;
29        end;
30      end;
31    until
32      False;
33    FindCloseChangeNotification(WaitHandle);
34  end;


			
Vote: How useful do you find this Article/Tip?
Bad Excellent
1 2 3 4 5 6 7 8 9 10

 

Advertisement
Share this page
Advertisement
Download from Google

Copyright © Mendozi Enterprises LLC