Improve company productivity with a Business Account.Sign Up


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 39

Expert Comment

by:Geert G
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 39

Accepted Solution

Geert G 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
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.


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 39

Expert Comment

by:Geert G
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
From store locators to asset tracking and route optimization, learn how leading companies are using Google Maps APIs throughout the customer journey to increase checkout conversions, boost user engagement, and optimize order fulfillment. Powered …
Wrapper-1-Query. Use an Excel function to calculate a column for an Access query. Part 1. Shows a query in Access that has a calculated column with the results of an Excel worksheet function. See how to call a wrapper function from a query, and …

579 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