I have the following code:
var items = new List<string> {"1", "2", "3"}; // 200 items foreach(var item in items) { ThreadPool.QueueUserWorkItem((DoWork), item); } private void DoWork(object obj) { lock(this) { using(var sw = File.AppendText(@"C:\somepath.txt") { sw.WriteLine(obj); } } }
Due to streaming, for some reason, I get a random number of 200 items written to a file. 60 or 127 or sometimes only 3. If I delete ThreadPool and just write inside the source foreach loop, all 200 elements will be successfully written.
Not sure why this is happening?
Thank you for your help.
Scott
source share