I'm working with multithreading and fell into a competition case. More than one thread accessed an I / O operation on the same file and so an exception was thrown. 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 lock
. I learned it as if it were a cake recipe (which is awful!). I do not know why I need the object padlock
.