I'm migrating a VCL app to FMX Mac OS.

I'm migrating a VCL app to FMX Mac OS.

Our application opens .png files that newly appear in a specific folder. These are created by a third party app. We display them in a TImage as "pseudo-video" at about 10 fps. We don't mind if we drop files now and then.

In Windows, we chose not to use the Windows hooks that are available to monitor folder events. We simply use a TTimer, calling TDirectory.GetFiles once a second and looking for a file we haven't already processed. We delete files after we've processed them, so the file list doesn't get too big.

In Windows, before actually opening an image for reading, we check if it is still open by the third party app with this code:

function FileClosed( const aFileName: String):Boolean;
var
hInputFile: THandle;
begin
hInputFile := CreateFile(Pchar(aFilename), GENERIC_WRITE, 0, nil,
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
Result := (hInputFile <> INVALID_HANDLE_VALUE);
if Result then
CloseHandle(hInputFile);
end;

Any suggestions on an efficient way to do the same thing in Mac OS: check if a file is open? It can be any method, even renaming the file.

Isof is too slow. I'm also hesitant to use any low-level Mac OS API, mainly because they generally require more OS and syntax and API savvy than I have.

We are just reading the file. And the third party is not re-opening the file, so we don't have any synchronization or race conditions to worry about.

Comments

  1. From my experience i would say, stop using these old file-routines and use Streams. I'm not even sure that they should fully work on MacOS.
    Use system.Ioutils :
    TFile.Move(Oldname, newname); These will raise a Exception if it fails.

    ReplyDelete
  2. those Tfile routines on OSX burb badly if the file does not exist I have found

    ReplyDelete
  3. Even Apple suggests (though it was 11 years ago) attempting the file operation, and dealing with the error if it occurs:

    lists.apple.com - Apple

    ReplyDelete

Post a Comment