Link to home
Start Free TrialLog in
Avatar of bish wakim
bish wakim

asked on

FileInUse Problem!

My Application uses FdMemTable and load a file called users.xml. every thing is nice until I the application is used by local network.
Several users try to open the same file at the same time! An exception rises!
I found a function that might help me but "createFile" is  not recognized also GENERIC_READ or GENERIC_WRITE  by the compiler. Please Help!  

function TDataContainer.IsFileInUse(fName : string) : boolean;
var //fNAME is the adress of the file to check if is in use
HFileRes : Thandle;
begin
 Result := false;
 if not FileExists(fName) then exit;
 HFileRes :=createfile(pchar(fName),GENERIC_READ or GENERIC_WRITE,0, nil, OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,0);
 Result := (HFileRes = INVALID_HANDLE_VALUE) ;
end;
Avatar of Geert G
Geert G
Flag of Belgium image

well yeah.
a file is like a narrow door, only 1 person can pass through it at a time

if you want multiple users to work on the same data at the same time.
Use a database, that's what those things were invented for

In essence, a database reads/writes from/to a set of files and coördinates all user actions to these files

a check for multiple users ...
place 2 copies of your code next to each other (in your mind), and run them line by line simultaneously.

first time:
first line:
result False;
second line:
> does the file exist? : answer no. >> Exit
Nothing gets past this ... if the file doesn't exist
third line, if the file exists, open it for writing ... multiple times

that third line doesn't work multiple times for multiple processes.
as i said a database coördinates this into 1 process.
Avatar of bish wakim
bish wakim

ASKER

Thank you for your response
however I imagined a way each time a call for this file is done to make a check if it is in use. if it is in use then wait until this file is freed.
Please look at the example(I gave in my first post) I could find for this  issue. it seems that it works but I don't understand why Delphi 10 seatle 10 vcl does not recognize creatfile function and some other flags.
Please I agree with you that DataBase is the ideal solution, but as a matter of fact I wish succeed with this with 10 users using my application    through local home network... they edit and update a single  file ...
Please Help
ASKER CERTIFIED SOLUTION
Avatar of Geert G
Geert G
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Please I have my reasons to go on with my choice. Let me limite my question, the solution Iam looking for is presented by the code bellow which I found some where and it is supposed to work. My only problem for the moment is with the function "creatfile". I still dont understand why My compiler (delphi 10 seatled) does not accepte it , maybe  should I add some unit to make this work!?

Function FileInUse(fName:string):boolean;
var   HFileRes : Thandle;
begin
 Result := false;
 if not FileExists(fName) then exit;
 HFileRes :=createfile(pchar(fName),GENERIC_READ or GENERIC_WRITE,0, nil, OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,0);
 Result := (HFileRes = INVALID_HANDLE_VALUE) ;
end;
If you havent implemented an insertedit correctly ... your app will never be for multiple users
Yes I think you are right. I should use DataBase...
Thank you!