werehamster-
asked on
1 MDI Form, 2 Indy Reader Threads, 1 INIFile. How do I make it thread safe?
Greetings,
This question spawns off of from another question...
https://www.experts-exchange.com/questions/21136526/Properly-Coded-Indy-Reader-Threads-Part-3.html
Right now I have an Indy Reader Thread, that spawns a 2nd Indy Reader Thread to a 2nd Login Server. Both threads need information from an INI file. Right now, the INI file is a property of the form which uses the INI also. I have tried using a critical section around all the reads to it, but it still does not work properly.
Here is a link to the source that I am using...
http://www.datazap.net/ftp/werehamster/MyBot/3.zip
Please, I need example source of how to use a single INI file between multible threads and a form. This is a critical part of my program and is only used during login so performance isn't an issue.
This question spawns off of from another question...
https://www.experts-exchange.com/questions/21136526/Properly-Coded-Indy-Reader-Threads-Part-3.html
Right now I have an Indy Reader Thread, that spawns a 2nd Indy Reader Thread to a 2nd Login Server. Both threads need information from an INI file. Right now, the INI file is a property of the form which uses the INI also. I have tried using a critical section around all the reads to it, but it still does not work properly.
Here is a link to the source that I am using...
http://www.datazap.net/ftp/werehamster/MyBot/3.zip
Please, I need example source of how to use a single INI file between multible threads and a form. This is a critical part of my program and is only used during login so performance isn't an issue.
ASKER
Hmm, never used this before...
TMultiReadExclusiveWriteSy nchronizer ;
I assume it is better than TCriticalSection?
So basically I treat is as .enter and .leave, but for file access stuff?
TMultiReadExclusiveWriteSy
I assume it is better than TCriticalSection?
So basically I treat is as .enter and .leave, but for file access stuff?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
I think I gave a correct solution.
ASKER
Shrug, did it myself. :)
At this moment the use of the INI file is threadsafe because you only use it in the FormCreate of TWereBot if you want to start using it from one of the threads as well it might be a problem.
You could make a threadsafe wrapper around the INI:
unit Unit1;
interface
uses
IniFiles, SysUtils;
type
TSafeIniFile = class(TIniFile)
private
fGuard: TMultiReadExclusiveWriteSy
public
constructor Create(const FileName: string);
destructor Destroy; override;
function ReadString(const Section, Ident, Default: string): string; override;
procedure WriteString(const Section, Ident, Value: string); override;
end;
implementation
{ TSafeIniFile }
constructor TSafeIniFile.Create(const FileName: string);
begin
inherited Create(FileName);
fGuard := TMultiReadExclusiveWriteSy
end;
destructor TSafeIniFile.Destroy;
begin
fGuard.Free;
inherited Destroy;
end;
function TSafeIniFile.ReadString(co
begin
fGuard.BeginRead;
try
Result := inherited ReadString(Section, Ident, Default);
finally
fGuard.EndRead;
end;
end;
procedure TSafeIniFile.WriteString(c
begin
fGuard.BeginWrite;
try
inherited WriteString(Section, Ident, Value);
finally
fGuard.EndWrite;
end;
end;
end.
You should also protect:
procedure ReadSection(const Section: string; Strings: TStrings); override;
procedure ReadSections(Strings: TStrings); override;
procedure ReadSectionValues(const Section: string; Strings: TStrings); override;
inside BeginRead/EndRead's and
procedure EraseSection(const Section: string); override;
procedure DeleteKey(const Section, Ident: String); override;
procedure UpdateFile; override;
inside BeginWrite/EndWrites
Now all threads including the forms main thread can share in instance of the INIFile
Regards Jacco