Andrew Thomas

Developer

CTO

Skier

Blog Post

Beware of using File/Stream WriteLineAsync methods

Aug 30, 2020 C#

Recently I have been getting into a lot of async coding, and really like it once I got my head around it. I try to use async methods whenever I can.

I was interested to see that that File and Stream methods have async methods too, and although I thought might be useful I also thought there would be a lot of inherit risk if not used properly. A small part of me thought there might be some internal synchronization in the classes though, in the situation where the method was called many times.

I wanted to confirm so created a console application that would write to a file async within a loop and see the outcome:

static void Main(string[] args)
{
 AsyncContext.Run(WriteFile);
}

private static async Task WriteFile()
{
 string file = Path.GetTempFileName();
 using (FileStream stream = new FileStream(file, FileMode.Create, FileAccess.ReadWrite, FileShare.Read, 4096, FileOptions.DeleteOnClose))
 {
 using (StreamWriter streamWriter = new StreamWriter(stream))
 {
 List<Task> tasks = new List<Task>();
 for (int i = 0; i < 1000; i++)
 tasks.Add(WriteFileLine(streamWriter));
 await Task.WhenAll(tasks);
 }
 }
 Console.WriteLine(File.ReadAllText(file));
}

private static async Task WriteFileLine(StreamWriter streamWriter)
{
 await streamWriter.WriteLineAsync(Test 123 456 789);
 await streamWriter.FlushAsync();
}

Unfortunately this crashed with the error: The stream is currently in use by a previous operation on the stream. So it seems that if you use WriteLineAsync you need to be very careful you don’t use that stream in any other code that runs whilst waiting for that async write to complete.