FileShare.None will throw a "System.IO.IOException" error if another thread tries to access the file.
You can use some function using try / catch to wait for the file to exit. An example is here .
Or you can use the lock statement with some dummy variable before accessing the write function:
// The Dummy Lock public static List<int> DummyLock = new List<int>(); static void Main(string[] args) { MultipleFileWriting(); Console.ReadLine(); } // Create two threads private static void MultipleFileWriting() { BackgroundWorker thread1 = new BackgroundWorker(); BackgroundWorker thread2 = new BackgroundWorker(); thread1.DoWork += Thread1_DoWork; thread2.DoWork += Thread2_DoWork; thread1.RunWorkerAsync(); thread2.RunWorkerAsync(); } // Thread 1 writes to file (and also to console) private static void Thread1_DoWork(object sender, DoWorkEventArgs e) { for (int i = 0; i < 20; i++) { lock (DummyLock) { Console.WriteLine(DateTime.Now.ToString("dd/MM/yyyy hh:mm:ss") + " - 3"); AddLog(1); } } } // Thread 2 writes to file (and also to console) private static void Thread2_DoWork(object sender, DoWorkEventArgs e) { for (int i = 0; i < 20; i++) { lock (DummyLock) { Console.WriteLine(DateTime.Now.ToString("dd/MM/yyyy hh:mm:ss") + " - 4"); AddLog(2); } } } private static void AddLog(int num) { string logFile = Path.Combine(Environment.CurrentDirectory, "Log.txt"); string timestamp = DateTime.Now.ToString("dd/MM/yyyy hh:mm:ss"); using (FileStream fs = new FileStream(logFile, FileMode.Append, FileAccess.Write, FileShare.None)) { using (StreamWriter sr = new StreamWriter(fs)) { sr.WriteLine(timestamp + ": " + num); } } }
You can also use the "lock" operator in the actual write function (i.e. inside AddLog), and not in the background working functions.
David Refaeli May 28 '17 at 9:07 a.m. 2017-05-28 09:07
source share