I have a C# process that is multi threaded. The number of threads is dynamic, and it cannot be determined at compile time. These threads all write to a log file. Obviously I am seeing cases where the one thread throws an exception because it cannot open the file because one of the other threads has it. What is the simplest way I can queue the write requests to the file so that each threads write request will be handled? Order of the writes is not critical. That is, if the writes requests are reordered within a one second range it will not matter. As long as they are close to the second it will be good.
Also, all of the threads call the exact same function to write to the log file. Is there something I can do to this one method to enforce thread safety?
Thanks,
Matthew
use "lock" to lock the object while another process is writing on the log file.
and also because of lock, other requests will have to wait before they could
access the file (first-come, first-serve)