C# 如何修改循环中的队列集合?
我有一个场景,在这个场景中,我需要在处理完一个项目后立即删除队列中的一个项目。 我知道我不能在循环中从集合中删除项目,但我想知道 可以使用枚举器等完成 这只是一个抛出错误的基本示例 “枚举数实例化后,集合被修改。” 有什么建议吗?非常感谢 代码如下:C# 如何修改循环中的队列集合?,c#,C#,我有一个场景,在这个场景中,我需要在处理完一个项目后立即删除队列中的一个项目。 我知道我不能在循环中从集合中删除项目,但我想知道 可以使用枚举器等完成 这只是一个抛出错误的基本示例 “枚举数实例化后,集合被修改。” 有什么建议吗?非常感谢 代码如下: class Program { static void Main() { Queue<Order> queueList = Ge
class Program
{
static void Main()
{
Queue<Order> queueList = GetQueueList();
foreach (Order orderItem in queueList)
{
Save(orderItem);
Console.WriteLine("Id :{0} Name {1} ", orderItem.Id, orderItem.Name);
queueList.Dequeue();
}
Console.Read();
}
private static void Save(Order orderItem)
{
//we are pretending to save or do something.
}
private static Queue<Order>GetQueueList()
{
Queue<Order> orderQueue = new Queue<Order>();
orderQueue.Enqueue(new Order { Id = 1, Name = "Order 1" });
orderQueue.Enqueue(new Order { Id = 1, Name = "Order 2" });
orderQueue.Enqueue(new Order { Id = 2, Name = "Order 3" });
orderQueue.Enqueue(new Order { Id = 3, Name = "Order 4" });
orderQueue.Enqueue(new Order { Id = 4, Name = "Order 5" });
return orderQueue;
}
}
public class Order
{
public int Id { get; set; }
public string Name { get; set; }
}
类程序
{
静态void Main()
{
队列列表=GetQueueList();
foreach(队列列表中的订单项)
{
保存(订单项);
WriteLine(“Id:{0}名称{1}”,orderItem.Id,orderItem.Name);
queueList.Dequeue();
}
Console.Read();
}
私有静态无效保存(订单项)
{
//我们假装在存钱或做些什么。
}
私有静态QueueGetQueueList()
{
Queue orderQueue=新队列();
Enqueue(新订单{Id=1,Name=“订单1”});
Enqueue(新订单{Id=1,Name=“Order 2”});
Enqueue(新订单{Id=2,Name=“订单3”});
Enqueue(新订单{Id=3,Name=“Order 4”});
Enqueue(新订单{Id=4,Name=“订单5”});
返回订单队列;
}
}
公共阶级秩序
{
公共int Id{get;set;}
公共字符串名称{get;set;}
}
将您的foreach更改为:
while (queueList.Count > 0)
{
Order orderItem = queueList.Dequeue();
Save(orderItem);
Console.WriteLine("Id :{0} Name {1} ", orderItem.Id, orderItem.Name);
}
编辑:
要在保存失败时重新处理,请执行以下操作:
while (queueList.Count > 0)
{
Order orderItem = queueList.Dequeue();
if (!Save(orderItem))
{
queueList.Enqueue(orderItem); // Reprocess the failed save, probably want more logic to prevent infinite loop
}
else
{
Console.WriteLine("Successfully saved: {0} Name {1} ", orderItem.Id, orderItem.Name);
}
}
编辑:
John K提到,如果有多个线程访问同一队列
,那么线程安全是一个值得关注的问题。有关涵盖简单线程安全问题的ThreadSafeQueue
类,请参阅
Edit:下面是我一直向大家指出的线程安全示例:-) 下面是一个提到的线程安全问题的示例。如图所示,默认的
队列
可以在减少计数的同时“错过”项目
更新:以更好地表示问题。我从不向队列添加空项,但标准的队列.Dequeue()
返回几个空值。仅此一项就可以了,但这样做会从内部集合中删除一个有效项,并且计数会减少。在这个特定示例中,可以安全地假设从队列返回的每个null
项。Dequeue()
操作表示从未处理过的有效项
using System;
using System.Collections.Generic;
using System.Threading;
namespace SO_ThreadSafeQueue
{
class Program
{
static int _QueueExceptions;
static int _QueueNull;
static int _QueueProcessed;
static int _ThreadSafeQueueExceptions;
static int _ThreadSafeQueueNull;
static int _ThreadSafeQueueProcessed;
static readonly Queue<Guid?> _Queue = new Queue<Guid?>();
static readonly ThreadSafeQueue<Guid?> _ThreadSafeQueue = new ThreadSafeQueue<Guid?>();
static readonly Random _Random = new Random();
const int Expected = 10000000;
static void Main()
{
Console.Clear();
Console.SetCursorPosition(0, 0);
Console.WriteLine("Creating queues...");
for (int i = 0; i < Expected; i++)
{
Guid guid = Guid.NewGuid();
_Queue.Enqueue(guid);
_ThreadSafeQueue.Enqueue(guid);
}
Console.SetCursorPosition(0, 0);
Console.WriteLine("Processing queues...");
for (int i = 0; i < 100; i++)
{
ThreadPool.QueueUserWorkItem(ProcessQueue);
ThreadPool.QueueUserWorkItem(ProcessThreadSafeQueue);
}
int progress = 0;
while (_Queue.Count > 0 || _ThreadSafeQueue.Count > 0)
{
Console.SetCursorPosition(0, 0);
switch (progress)
{
case 0:
{
Console.WriteLine("Processing queues... |");
progress = 1;
break;
}
case 1:
{
Console.WriteLine("Processing queues... /");
progress = 2;
break;
}
case 2:
{
Console.WriteLine("Processing queues... -");
progress = 3;
break;
}
case 3:
{
Console.WriteLine("Processing queues... \\");
progress = 0;
break;
}
}
Thread.Sleep(200);
}
Console.SetCursorPosition(0, 0);
Console.WriteLine("Finished processing queues...");
Console.WriteLine("\r\nQueue Count: {0} Processed: {1, " + Expected.ToString().Length + "} Exceptions: {2,4} Null: {3}", _Queue.Count, _QueueProcessed, _QueueExceptions, _QueueNull);
Console.WriteLine("ThreadSafeQueue Count: {0} Processed: {1, " + Expected.ToString().Length + "} Exceptions: {2,4} Null: {3}", _ThreadSafeQueue.Count, _ThreadSafeQueueProcessed, _ThreadSafeQueueExceptions, _ThreadSafeQueueNull);
Console.WriteLine("\r\nPress any key...");
Console.ReadKey();
}
static void ProcessQueue(object nothing)
{
while (_Queue.Count > 0)
{
Guid? currentItem = null;
try
{
currentItem = _Queue.Dequeue();
}
catch (Exception)
{
Interlocked.Increment(ref _QueueExceptions);
}
if (currentItem != null)
{
Interlocked.Increment(ref _QueueProcessed);
}
else
{
Interlocked.Increment(ref _QueueNull);
}
Thread.Sleep(_Random.Next(1, 10)); // Simulate different workload times
}
}
static void ProcessThreadSafeQueue(object nothing)
{
while (_ThreadSafeQueue.Count > 0)
{
Guid? currentItem = null;
try
{
currentItem = _ThreadSafeQueue.Dequeue();
}
catch (Exception)
{
Interlocked.Increment(ref _ThreadSafeQueueExceptions);
}
if (currentItem != null)
{
Interlocked.Increment(ref _ThreadSafeQueueProcessed);
}
else
{
Interlocked.Increment(ref _ThreadSafeQueueNull);
}
Thread.Sleep(_Random.Next(1, 10)); // Simulate different workload times
}
}
/// <summary>
/// Represents a thread safe <see cref="Queue{T}"/>
/// </summary>
/// <typeparam name="T"></typeparam>
public class ThreadSafeQueue<T> : Queue<T>
{
#region Private Fields
private readonly object _LockObject = new object();
#endregion
#region Public Properties
/// <summary>
/// Gets the number of elements contained in the <see cref="ThreadSafeQueue{T}"/>
/// </summary>
public new int Count
{
get
{
int returnValue;
lock (_LockObject)
{
returnValue = base.Count;
}
return returnValue;
}
}
#endregion
#region Public Methods
/// <summary>
/// Removes all objects from the <see cref="ThreadSafeQueue{T}"/>
/// </summary>
public new void Clear()
{
lock (_LockObject)
{
base.Clear();
}
}
/// <summary>
/// Removes and returns the object at the beggining of the <see cref="ThreadSafeQueue{T}"/>
/// </summary>
/// <returns></returns>
public new T Dequeue()
{
T returnValue;
lock (_LockObject)
{
returnValue = base.Dequeue();
}
return returnValue;
}
/// <summary>
/// Adds an object to the end of the <see cref="ThreadSafeQueue{T}"/>
/// </summary>
/// <param name="item">The object to add to the <see cref="ThreadSafeQueue{T}"/></param>
public new void Enqueue(T item)
{
lock (_LockObject)
{
base.Enqueue(item);
}
}
/// <summary>
/// Set the capacity to the actual number of elements in the <see cref="ThreadSafeQueue{T}"/>, if that number is less than 90 percent of current capactity.
/// </summary>
public new void TrimExcess()
{
lock (_LockObject)
{
base.TrimExcess();
}
}
#endregion
}
}
}
使用系统;
使用System.Collections.Generic;
使用系统线程;
名称空间SO_线程安全队列
{
班级计划
{
静态int_队列异常;
静态int QueueNull;
已处理的静态int_队列;
静态int_线程安全队列异常;
静态int_ThreadSafeQueueNull;
已处理静态int_线程;
静态只读队列_Queue=new Queue();
静态只读ThreadSafeQueue _ThreadSafeQueue=new ThreadSafeQueue();
静态只读随机_Random=new Random();
预期常数int=10000000;
静态void Main()
{
Console.Clear();
Console.SetCursorPosition(0,0);
Console.WriteLine(“创建队列…”);
对于(int i=0;i0 ||_ThreadSafeQueue.Count>0)
{
Console.SetCursorPosition(0,0);
开关(进度)
{
案例0:
{
Console.WriteLine(“处理队列…|”);
进展=1;
打破
}
案例1:
{
Console.WriteLine(“处理队列…/”);
进展=2;
打破
}
案例2:
{
Console.WriteLine(“处理队列…-”);
进展=3;
打破
}
案例3:
{
WriteLine(“处理队列…\ \”;
进度=0;
打破
}
}
睡眠(200);
}
Console.SetCursorPosition(0,0);
控制台.WriteLine(“已完成处理队列…”);
Console.WriteLine(“\r\n队列计数:{0}已处理:{1,+Expected.ToString().Length+”}异常:{2,4}Null:{3}”,_Queue.Count,_QueueProcessed,_QueueExceptions,_QueueNull);
WriteLine(“ThreadSafeQueue计数:{0}已处理:{1,+Expected.ToString().Length+”}异常:{2,4}Null:{3},_ThreadSafeQueue.Count,_ThreadSafeQueueProcessed,_ThreadSafeQueueExceptions,_ThreadSafeQueueNull);
Console.WriteLine(“\r\n按任意键…”);
Console.ReadKey();
}
斯达
// the non-thread safe way
//
while (queueList.Count > 0)
{
Order orderItem = queueList.Dequeue();
Save(orderItem);
Console.WriteLine("Id :{0} Name {1} ", orderItem.Id, orderItem.Name);
}
// the thread safe way.
//
while (true)
{
Order orderItem = NULL;
try { orderItem = queueList.Dequeue(); } catch { break; }
if (null != OrderItem)
{
Save(orderItem);
Console.WriteLine("Id :{0} Name {1} ", orderItem.Id, orderItem.Name);
}
}