C#使用队列(Queue)解决简单的并发问题

2019-12-26 13:02:57王振洲
  • queueinfo.medias = medias;   queueinfo.proids = proids;  
  • queueinfo.host = host;   queueinfo.userid = userid;  
  • queueinfo.feedid = feedid;   ListQueue.Enqueue(queueinfo);  
  • }    
  • public void Start()//启动   {  
  • Thread thread = new Thread(threadStart);   thread.IsBackground = true;  
  • thread.Start();   }  
  •   private void threadStart()  
  • {   while (true)  
  • {   if (ListQueue.Count > 0)  
  • {   try 
  • {   ScanQueue();  
  • }   catch (Exception ex)  
  • {   LO_LogInfo.WLlog(ex.ToString());  
  • }   }  
  • else  {  
  • //没有任务,休息3秒钟   Thread.Sleep(3000);  
  • }   }  
  • }    
  • //要执行的方法   private void ScanQueue()  
  • {   while (ListQueue.Count > 0)  
  • {   try 
  • {   //从队列中取出  
  • QueueInfo queueinfo = ListQueue.Dequeue();    
  • //取出的queueinfo就可以用了,里面有你要的东西   //以下就是处理程序了  
  • //。。。。。。    
  • }   catch (Exception ex)  
  • {   throw;  
  • }   }  
  • }    
  •   #endregion  
  • }   }