lock statement

  • Thread starter Thread starter Jeff0803
  • Start date Start date
J

Jeff0803

Guest
I have two questions regarding lock statement.

I write log to the text file like following.


private readonly object locker = new object();
.
.
.
public void WriteLog(string strlog)
{
lock (locker)
{
using (FileStream file = new FileStream(applicationpath + @"\Log\" + DateTime.Now.Date.ToString("MMddyyyy") + ".log", FileMode.Append, FileAccess.Write, FileShare.Read))
using (StreamWriter w = new StreamWriter(file, Encoding.Unicode))
{
w.WriteLine(DateTime.Now.ToLongTimeString() + "\t" + strlog);
}
}
}


1. Somebody says the object used in the lock statement should be static readonly but MSDN shows readonly. which one is correct?

2. Is that all? Don't I have to release lock in any way?

Continue reading...
 
Back
Top