Wait till a file editing ends
I have next task. It is necessary to wait of end of editing Microsoft Office files.I use next conventional approach: file is editing开发者_高级运维 While file is busy:
function FileIsBusy(AFileName: string): Boolean;
var
F: Integer;
begin
F := FileOpen(AFileName, fmShareExclusive);
Result := F = -1;
FileClose(F);
end;
function WaitFile(AFileName: string; ASpeepDelay: integer): Boolean;
begin
while FileIsBusy(AFileName) do
Sleep(ASpeepDelay);
Result := True;
end;
This approach good work with editing file by Microsoft Word, not in Open Office. OpenOffice use one process for open multiplicity files just like MS Office. But with OpenOffice Writer have some problems: function FileIsBusy return false just after first saving file by OpenOffice Writer. Have anything suggestions?
============================
I find next solution:
repeat
WaitFile(FFileInfo.lpFile, 333);
Sleep(1000);
until not FileIsBusy(FFileInfo.lpFile);
After preservation, ОО releases a file for some time and again exclusively share
I don't agree that your approach would be the conventional approach
.
Polling should not be the convention
, it should be a last resort
.
Different office suites will use different ways of writing to files.
Some will write to a new file, and rename that file upon completion.
Some will rename the old file, then write the file.
Some might perform a lock.
You should find out how your office suites are going to write those files.
For that you need to monitor what is happening.
Windows has the ReadDirectoryChangesW function for that, and OSnews published a nice A Directory Monitor Class For Delphi which wraps around that. There is also a nice MSDN FWatch example that explains how to use ReadDirectoryChangesW
.
Incidently, when you understand how which office suite writes the files, you can use your ReadDirectoryChangesW
code to watch for those file writes too.
--jeroen
精彩评论