Share via

Wait for StreamWriter.FlushAsync to complete

Frank Natoli 71 Reputation points
2021-03-10T11:10:36.473+00:00

Have C# WPF application that continuously appends to various log files.
Ran into trouble when customer's backup program was interfering with access to the log files.
Apparently, Windows SMB layer was often causing ten seconds or more delay for file open, then failing because backup program had opened the file with exclusive access.
Modified C# application to keep logs open, in FileMode.Append, FileAccess.Write and FileShare.Read.
After appending to log, I tried calling StreamWriter.Flush but files were zero length and unreadable by other processes.
I then tried calling StreamWriter.FlushAsync, and now files are non-zero length, are readable by other processes, but flush is of course happening asynchronously and my own process can throw an exception if it attempts to again append to a log that is still flushing asynchronously.
How to wait for StreamWriter.FlushAsync to complete?

Developer technologies | Windows Presentation Foundation
Developer technologies | .NET | Other

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.