我有多个backgroundworkers都想写入 log.txt,这会导致异常The process cannot access the file 'C:\...\log.txt' because it is being used by another process.。我知道这是一个很长的镜头,但如果我使用它会有所帮助WriteAsync()还是根本没有效果?
(如果这不是一个简单的解决方案,我想我必须实现mutex我以前见过的对象。)
public static void WriteToLog(string text, bool append = true)
{
try
{
using (var writer = new StreamWriter("log.txt", append))
{
writer.Write(text);
// writer.WriteAsync(text); // Would this 'queue up' instead of trying
to access the same process at the same time?
}
}
catch (Exception ex)
{
Console.WriteLine($"ERROR! Fejl i loggen! {ex.Message}. {ex.StackTrace}");
}
}
富国沪深
眼眸繁星
子衿沉夜
相关分类