对写文本日志进行同步,防止并发操作IO异常

阅读更多

static object obj = new object();
 static void log(string message)
        {
            lock (obj)
            {
                using (FileStream fsError = new FileStream("C:\\testlog.txt", FileMode.Append, FileAccess.Write, FileShare.Write))
                {
                    using (StreamWriter swError = new StreamWriter(fsError, Encoding.Default))
                    {
                        swError.WriteLine(message);
                    }
                }
                Monitor.PulseAll(obj);
                Monitor.Wait(obj);
            }
        }

你可能感兴趣的:(C,C++,C#)