Is TMemIniFile thread-safe?
A user is getting an intermittent error "Cannot create file "C:...\Filename.ini". The requested operation cannot be performed on a file with a user-map开发者_如何学编程ped section open."
I haven't been able to find much about this error that helps understand what's going on.
Is TMemIniFile thread-safe?
As far as I know TMemIniFile (and any other TCustomIniFile descendants) is not thread-safe. You will need to wrap it into a critical section.
In this link you can find an implementation of a thread-safe TCustomIniFile (theoretically programmed by Peter Below of TeamB though I can not assure it).
There is also a discussion in the Embarcadero forums about the thread-safety of TMemIniFile here. They speak about the C++ version of the component.
You can also find a discussion in MSDN regarding the origin of your error message here. It is a bit long but the general conclussion is that there are 2 exes trying to acces the same file. You can find another discussion on the subject here.
First of all, I am assuming that the particular threading configuration that is causing the problem reported is that there are multiple TMemIniFile
instances, possibly even in different processes, being saved simultaneously from different threads.
TMemIniFile
is not thread-safe. To avoid any race conditions you need to write (pseudo-)code like this:
AcquireLock;
Try
ReadMemIniFileFromDisk;
ModifyMemIniFileInMemory;
WriteMemIniFileToDisk;
Finally
ReleaseLock;
End;
It's not enough to lock around just the file operations because then you may lose changes due to a race. You have to lock the entire read/modify/write cycle.
Solution
This error message is caused by a bug in AVG. Apparently in its real-time file-access monitoring mode, AVG opens some files in exclusive mode, preventing others from writing to them.
The fix was to configure AVG to ignore the folder in which this file resides.
Thanks, AVG! Can I have my 2 days of wasted time back now? :-)
Thanks to everyone who answered here.
Tom
精彩评论