question

FrankNatoli-2507 avatar image
0 Votes"
FrankNatoli-2507 asked ·

Wait for StreamWriter.FlushAsync to complete

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?

windows-wpfdotnet-standard
· 1
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

@FrankNatoli-2507
Do you want your log file to be accessible to both backup program and C# WPF application at the same time? Did you use the StreamWriter.FlushAsync in the Task and want to get the Result for the Task? What exception do you meet? Could you give me more detailed info for code snippet and the exception?

0 Votes 0 ·

0 Answers