本文实例讲述了C#队列Queue多线程用法。分享给大家供大家参考。具体分析如下:
这里展示一个例子,供学习使用:
private void button_测试Queue结合多线程_Click(object sender, EventArgs e){ Console.WriteLine("初始化队列"); queue = new Queue<string>(); string[] cars = new string[]{"宝马","奔驰", "奥迪","东风","劳斯莱斯"}; foreach (string str in cars) { queue.Enqueue(str); Console.WriteLine("入队列-{0}", str); } Console.WriteLine(); Console.ForegroundColor = ConsoleColor.Red; Thread th = new Thread(new ThreadStart(printQueue)); th.IsBackground = true; //后台运行,主窗体关闭后,可退出程序 th.Start();}private void printQueue(){ while (true) { if (queue.Count > 0) { Console.WriteLine("出队列-{0}", queue.Dequeue()); } }}
运行结果
希望本文所述对大家的C#程序设计有所帮助。
新闻热点
疑难解答