Go Premium for a chance to win a PS4. Enter to Win


FileInUse Problem!

Posted on 2017-10-01
Low Priority
Last Modified: 2017-10-22
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;
 Result := false;
 if not FileExists(fName) then exit;
 Result := (HFileRes = INVALID_HANDLE_VALUE) ;
Question by:bish wakim
  • 3
  • 3
LVL 38

Expert Comment

by:Geert Gruwez
ID: 42315327
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.

Author Comment

by:bish wakim
ID: 42315819
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
LVL 38

Accepted Solution

Geert Gruwez earned 1000 total points
ID: 42316471
have you ever considered writing in multiple places in the same file ?

there is no insert mode in a file ... unless you use a record structure (xml is not a record structure)
there is only overwrite mode in a file

an unpacked record structure reserves spaces for each entry
sample: first name: 50 char, last name 50 char
in the file, you would see x chars filled in the first name place and 50-x blancs
and so on for the last name

xml does not have this structure, every character you see on the file is physically next to the previous character
so if you were change a typo like "Cliton" to "Clinton",
your app would have to read the whole XML file,
and write everything into a new XML file
and then rename the new file into the old.

i know a few companies who have up to now failed at implementing a multi-user app even with an oracle database as the backend
they had vast locking problems from 6+ users, and this is a big company !

you obviously started designing your app with just you as the user: in other words: 1 user
and then found out it doesn't work in a multi-user environment

the first thing to look at
1: consistency
how to ensure the data remains consistent with multiple users
> indeed you need a locking mechanism, but it needs a little more depth than just a boolean value
2: storage
with multiple users, you need a shared storage space for the data
> if the user with the database on his pc switches it off ... complaints will follow rather swiftly

10 users might not seem much, but it is more than 1... and you only need 2 users for a deadlock

XML is not a database, it's a file with a specific structure and rules to pass between apps
any app following the rules can read the file and extract the data correctly

there are loads of free databases out there
look on google for the express versions


Delphi has the tools built in ... FireDac to connect with all these databases.

if you really want to stick with files ...
can go the n-tier way.

Every user connects to a intermediate layer.  This layer reads/writes to the file.
And all requests are typically processed sequentially

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.


Author Comment

by:bish wakim
ID: 42316502
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;
 Result := false;
 if not FileExists(fName) then exit;
 Result := (HFileRes = INVALID_HANDLE_VALUE) ;
LVL 38

Expert Comment

by:Geert Gruwez
ID: 42330623
If you havent implemented an insertedit correctly ... your app will never be for multiple users

Author Comment

by:bish wakim
ID: 42332034
Yes I think you are right. I should use DataBase...
Thank you!

Featured Post

[Webinar] Cloud Security

In this webinar you will learn:

-Why existing firewall and DMZ architectures are not suited for securing cloud applications
-How to make your enterprise “Cloud Ready”, and fix your aging DMZ architecture
-How to transform your enterprise and become a Cloud Enabler

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
Suggested Courses

971 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question