I put lock on a few lines which are supposed to get cookie files and read them, but I see sometimes an error saying file already in use! So not sure whats going wrong…
Code:
private Object cookieLock = new Object();
main{
for (int j = 0; j < maxThreads; j++)
{
// Thread thread = new Thread(new ThreadStart(startPosting2(worker)));
Thread thread = new Thread(() => postingFunction2());
thread.IsBackground = true;
thread.Start();
}
}
public void postFunction2()
{
string tmpUsername = string.Empty;
string[] array2 = null;
try
{
lock (cookieLock)
{
array2 = File.ReadAllLines(tmpUsername + ".txt");
}
}
catch(Exception ex)
{
TextWriter sUrl = new StreamWriter("readingSameCookieFile.txt", true);
sUrl.WriteLine(exp.ToString());
sUrl.Close();
}
}
Am I doing anything wrong? These lines are executed by 20-100 threads simultaneously, I do not see it much but I do see it some time, so wondering why!
TXT FILE ERROR:
System.IO.IOException: The process cannot access the file'C:\Users\Administrator\My Projects\Bot Files\2 Message Poster Bot\MessagePoster - NoLog - Copy\cookies\LaureneZimmerebner57936.txt'because it is being used by another process.
If you just want to overcome the problem you are having and are sure it’s not beeing written then you can use this function instead of
File.ReadAllLines. The key is it the share options it givesFileShare.ReadWrite.