欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C#队列Queue多线程用法实例

程序员文章站 2022-04-29 21:01:56
本文实例讲述了c#队列queue多线程用法。分享给大家供大家参考。具体分析如下: 这里展示一个例子,供学习使用: private void button_测试q...

本文实例讲述了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#队列Queue多线程用法实例

希望本文所述对大家的c#程序设计有所帮助。