6
I’m working with multithreading and fell in a competition case. More than one thread accessed an I/O operation on the same file and so an exception was triggered. To solve the problem, I did so:
private static object padlock = new object();
private static void SaveFile(string content) {
lock (padlock) {
Directory.CreateDirectory(Dir);
File.AppendAllText(FilePath, content);
}
}
The object padlock
is passed to the lock
. I learned as if it were a cake recipe (which is horrible!). I don’t know why I need the object padlock
.