C# Threading/Lock confusion
问题 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); } } } Because of the threading, for some reason, I get a random number of the 200 items written out to the file. 60 or 127 or sometimes only 3. If I remove the ThreadPool and just write inside the original foreach