出于示例目的,我已简化此代码:
class TextLogger : IDisposable
{
private FileStream m_FileStream;
private StreamWriter m_StreamWriter;
void CreateNewLogFile()
{
//Open the File
m_FileStream = File.Open(
m_CurrentFileName,
FileMode.OpenOrCreate,
FileAccess.Write,
FileShare.Read );
m_StreamWriter = new StreamWriter( m_FileStream );
....
}
}
我InvalidArgumentException
在尝试更新 StreamWriter 时得到一个,因为m_FileStream
已经被另一个线程处理并且为空(m_StreamWriter
也是空的)。如何在成员变量周围加锁?