Newsgroups : Borland : borland.public.delphi.nativeapi.win32 : 2007 Mar : Multi Threaded File Access

www.cryer.info
Managed Newsgroup Archive

Multi Threaded File Access

Subject:Multi Threaded File Access
Posted by:"Doni Devito" (do..@devito.com)
Date:Wed, 21 Mar 2007 13:48:58

Hi,
I had a multi threaded application. Its very hard to debug multi threaded
application. For this reason I wrote a function which writes the text to a
text file. Every thread calls this function. I had a global parameter that
reads vaue from ini file. If I wrote DEBUG=TRUE to ini file, it begins to
write debug text to file.

I know that the idea is good and simple but, I cant write to text file in
multi threaded environment. My code is something like this;

     AssignFile(txt,FName);
     if FileExists(FName) then
        Append(Txt)
        else
        Rewrite(Txt);
     WriteLn(Txt,Msg);
     CloseFile(Txt);

Sometimes it raises I/O exception. I know that its because the file Assigned
already. How can I avoid from this situation? How can I write something to
same textfile from different threads at the same time?

Thanks.

By the way, the writing time is not important. Because Im running it only
when Im debugging the application.

Replies:

www.cryer.info
Managed Newsgroup Archive