我在 C# (4.0) 中有一个常规的队列对象,并且我正在使用访问此队列的 BackgroundWorkers。
我使用的代码如下:
do
{
while (dataQueue.Peek() == null // nothing waiting yet
&& isBeingLoaded == true // and worker 1 still actively adding stuff
)
System.Threading.Thread.Sleep(100);
// otherwise ready to do something:
if (dataQueue.Peek() != null) // because maybe the queue is complete and also empty
{
string companyId = dataQueue.Dequeue();
processLists(companyId);
// use up the stuff here //
} // otherwise nothing was there yet, it will resolve on the next loop.
} while (isBeingLoaded == true // still have stuff coming at us
|| dataQueue.Peek() != null); // still have stuff we haven’t done
但是,我想在处理线程时我应该使用ConcurrentQueue
. ConcurrentQueue
我想知道是否有像上面那样在 Do While 循环 中使用 a 的示例?
我用 TryPeek 尝试的一切都不起作用..
有任何想法吗?