As mentioned in the comments, this is asking for trouble.
So, you need to have a thread-safe writer class:
public class FileWriter
{
private ReaderWriterLockSlim lock_ = new ReaderWriterLockSlim();
public void WriteData(/*....whatever */)
{
lock_.EnterWriteLock();
try
{
// write your data here
}
finally
{
lock_.ExitWriteLock();
}
}
} // eo class FileWriter
This is suitable for being called by many threads. BUT, there's a caveat. There may well be lock contention. I used a ReadWriterLockSlim
class, because you may want to do read locks as well and hell, that class allows you to upgrade from a read state also.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…