Im using the following to empty a txt file:
System.IO.File.WriteAllText(path, string.Empty);
The problem becomes when i run to many threads at once and they try to access the file at the same time..
Any ideas on an easy fix? 🙂
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Multi-threading to the rescue!
You should use a
Monitor,AutoResetEventorlockblock (actually it’s a Monitor) in order to synchronize access to the whole file.}
This way you’re preventing multiple threads to access the same file at the same time.
Learn about
lockblock here:http://msdn.microsoft.com/en-us/library/c5kehkcz(v=vs.110).aspx