private static ReaderWriterLockSlim _readWriteLock;
public void WriteToFileThreadSafe(string text, string path)
{
// Set Status to Locked
_readWriteLock.EnterWriteLock();
try
{
// Append text to the file
using (StreamWriter sw = File.AppendText(path))
{
sw.WriteLine(text);
sw.Close();
}
}
finally
{
// Release lock
_readWriteLock.ExitWriteLock();
}
}
Leave a Reply