Help needed in thread

muralik

Member
Joined
Jun 14, 2003
Messages
5
My requirement:
I have more than one company in my database, which individually has more than 100 email-ids(i.e, client email-ids).
For each company I am creating a new thread and writing email-ids in a log file.

But the problem is, all the values are not written in the text file. Some data are missing, why it is so?

Code:
====
private void Page_Load(object sender, System.EventArgs e)
{
for (i=1; i<=10000; i++)
{
arrText.Add("email_" + i + "@hotmail.com");
}

sLogFilePath = "C:\\Documents and Settings\\NSPL\\Desktop\\logfile.txt";
FileStream fsLogFile = new FileStream(sLogFilePath, FileMode.OpenOrCreate, FileAccess.Write);
StreamWriter swLogFile = new StreamWriter(fsLogFile);
swLogFile.BaseStream.Seek(0, SeekOrigin.End);
swLogFile.WriteLine("Log file starts here\n");

for (i=1; i<=3; i++)
{
ThreadPool.QueueUserWorkItem(new WaitCallback(InstanceMethod), i.ToString());
}
}

public void InstanceMethod(object CompanyID)
{
for (j=0; j<arrText.Count; j++)
{
swLogFile.WriteLine(CompanyID.ToString() + " " + arrText[j].ToString() + "\n");
}
swLogFile.Flush();
}
 
Are all these threads writing to the same log file at the same time? If so you could be experiencing locking problems.

You might want to look at syncronising the threads or perhaps implement a single thread to do the file IO
 
Back
Top