class Program { static void Main() { var b1 = new BlockingCollection<int>(); ThreadPool.QueueUserWorkItem(ConSumeItems, b1); for (int i = 0; i < 5; i++) { Console.WriteLine("producing: " + i); b1.Add(i); } //告诉消费线程(可能有多个这样的线程),不会再集合中添加更多的项了 b1.CompleteAdding(); Console.ReadKey(); } private static void ConSumeItems(object o) { var b1 = (BlockingCollection<int>)o; //阻塞,直到出现一个数据项。出现后就处理它 foreach (var item in b1.GetConsumingEnumerable()) { Console.WriteLine("consuming :{0}", item); } //集合空白,没有更多的项进入其中 Console.WriteLine("All items have been consumed"); } }