Home > Back-end >  Delphi two programs at the same time, speaking, reading and writing the same file (TIniFile) failure
Delphi two programs at the same time, speaking, reading and writing the same file (TIniFile) failure

Time:09-20

A program every two seconds to read A file, another program often failed to write on A file at the same time, the file type is TIniFile, what is the simple way to solve this problem? thank you

CodePudding user response:

Don't write so often, if you don't want to establish communication between two programs, then simple point: use timer, reading time, seconds to analog 4 to 0 first program operational, seconds to analog 4 second program operational, premise: 1, each program can quickly after operation; 2, every time to read the initialization value,

CodePudding user response:

reference 1st floor leeky response:
don't write so often, if you don't want to establish communication between two programs, then simple point: use timer, reading time, seconds to analog 4 to 0 first program operational, seconds to analog 4 second program operational, premise: 1, each program can quickly after operation; 2, every time to read the initialization value,

Every second is the first program reads a and must be the first time, every second, a second program occasionally write operation,

CodePudding user response:

File open mode, program should use fmOpenReadWrite + fmShareDenyWrite writing files, read files of the program can use fmOpenRead + fmShareDenyNone

CodePudding user response:

reference DelphiGuy reply: 3/f
file open mode, program should use fmOpenReadWrite + fmShareDenyWrite writing files, read the file program can use fmOpenRead + fmShareDenyNone

I am using TIniFile TIniFile it opens the file constructor created (const FileName: String); Do not seem to support open mode Settings

CodePudding user response:

1, because when the INI file to read and write, is a quick read and write, so can't read and write operations, slowly,
2, the system does not allow two procedures, a file is read and write at the same time, otherwise, error,
3, if the score like this words, can by:
(1) in A program, set two Boolean; Types of variables,
Var F1, F2: Boolean; Types of variables,
F1 is true, A program to read and write files, otherwise it is false;
F2 is true, B program to read and write files, otherwise it is false;
In this way, by way of Shared memory, also read/write F B program, F2,
4, when A program when F2 is false, can read and write files, program B when F1 is false, can read and write files,

CodePudding user response:

Feels like the data transmission between process, if so, suggest using Shared memory + events; Pipeline, reliable, safe, timely, file to pass, not so good

CodePudding user response:

Program to read A file, can be A copy of A copy first and then read;
After the program B write, A notification program at the same time, you need to copy

CodePudding user response:

Also quickly read and write ini, the simplest is judging timer clock seconds,
Complex inter-process communication, or network communication...

CodePudding user response:

Read and write at the same time, it is suggested that through the exclusive lock lock handle

CodePudding user response:

In general, sharing conflict is by the system automatically judge and deal with better
The original poster can try to use the condition of the TMEMO LOADFROMFILE and SAVETOFILE method for processing solve sharing conflict, but for INI format operation again for processing

CodePudding user response:

The synchronization across processes is the best example! The kernel object!

CodePudding user response:

There is A way, through the standard time, allocated, such as A minute, before thirty seconds by A read/write INI file, 30 seconds after the program to read and write INI who B,

CodePudding user response:

Can read and write to write a DLL special INI file

CodePudding user response:

reference 5 floor lyhoo163 reply:
1, because when the INI file to read and write, is a quick read and write, so can't read and write operations, slowly,
2, the system does not allow two procedures, a file is read and write at the same time, otherwise, error,
3, if the score like this words, can by:
(1) in A program, set two Boolean; Types of variables,
Var F1, F2: Boolean; Types of variables,
F1 is true, A program to read and write files, otherwise it is false;
F2 is true, B program to read and write files, otherwise it is false;
In this way, by way of Shared memory, also read/write F B program, F2,
4, when A program when F2 is false, can read and write files, program B when F1 is false, can read and write files,
problem solving?

CodePudding user response:

Specific code?

CodePudding user response:

reference 15 floor woo_heilin response:
specific code?

You search, Shared memory!

CodePudding user response:

Use a memory mapped file, also not complicated

CodePudding user response:

Across the process synchronization with mutexes, semaphores,