I would like to ask help on my code. I am a newbie and wanted to implement safe multi threading in writing to a text file.
StreamWriter sw = new StreamWriter(@"C:\DailyLog.txt");
private void Update(){
var collection = Database.GetCollection<Entity>("products");
StreamReader sr = new StreamReader(@"C:\LUSTK.txt");
string[] line = sr.ReadLine().Split(new char[] { ';' });
while (!sr.EndOfStream)
{
line = sr.ReadLine().Split(new char[] { ';' });
t = delegate {
UpdateEach(Convert.ToInt32(line[5]));
};
new Thread(t).Start();
}
sr.Close();
}
private void UpdateEach(int stock)
{
sw.WriteLine(ean);
}
I got no error on my code but it seems not all written to my text file. I did not make sw.Close()
because i know some threads were not finish yet. In addition, how can i implement sw.Close
knowing that no thread left unfinish. I have 5 milion records in my LUSTK.text
that is to be read by StreamReader
and each created a thread and each thread access same text file.