I have an async write but I want to make sure if the method is called again while a write is still underway, it waits until the file is ready before trying the next attempt.
I want this all to be non blocking.
Is this the correct way to do it?
Do i even need the lock? Or does the streamwriter already look after this.
lock (fileWriterLock) { FileWriteAsync(filename, data); } and then..
public async Task FileWriteAsync(string filePath, string messaage) { using (FileStream stream = new FileStream(filePath, FileMode.Create, FileAccess.Write, FileShare.None, 4096, true)) using (StreamWriter sw = new StreamWriter(stream)) { await sw.WriteLineAsync(messaage); } }
FileSharevalue passed to theFileStreamconstructor will prevent concurrent access to the file, it doesn't cause any sort of wait/synchronization; another thread or process trying to open the file will just fail. ...