After a lot of research, and after reading and considering all the questions here, I think it's time for me to ask for help.
I have an application in C # and I'm trying to write a file with a different stream to SAME.
public static void LaunchThreads(string path_file) { int i = 0; Dictionary<int, Thread> threadsdico = new Dictionary<int, Thread>(); while (i < MAX_THREAD) { Thread thread = new Thread(() => ThreadEntryWriter(string path_file)); thread.Name = string.Format("ThreadsWriters{0}", i); threadsdico.Add(i, thread); thread.Start(); i++; } int zz = 0; while (zz < threadsdico.Count()) { threadsdico[zz].Join(); zz++; } } private static readonly Object obj = new Object(); public static void ThreadEntryWriter(string path_file) { int w = 0; while (w < 99) { string newline = w + " - test" + "\r"; lock(obj) { string txt = File.ReadAllText(path_file); using (TextWriter myWriter = new StreamWriter(path_file)) { TextWriter.Synchronized(myWriter).Write(txt + newline); } } w++; } }
I tried everything, my code around the world is similar, but I try my best, with every lock, every way to open a file, but I keep getting The process cannot access the files because it in use . The line that generates this error is this using (TextWriter myWriter = new StreamWriter(path_file)) .
I tried a lot, closing files, etc., but when the threads start working at the same time, the program stops and gives me an error The process cannot access the files because it in use (self-explain). But I donβt understand why blocking involves blocking another thread to enter here. And I used the Synchronized method for recording, which is thread safe. Well, sorry for writing this first post here.
source share