Как я могу изменить коллекцию очередей в цикле?
У меня есть сценарий, в котором мне нужно удалить элемент для очереди, как только будет обработан. Я понимаю, что не могу удалить элемент из коллекции, находясь в цикле, но мне было интересно, можно ли что-то сделать с помощью Enumerator и т. Д.
Это просто базовый пример, выдающий ошибку: "Коллекция была изменена после создания экземпляра перечислителя".
Какие-либо предложения? Большое спасибо!!!
Код выглядит следующим образом:
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; }
}
3 ответа
Измените ваш 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);
}
}
Редактировать:
Джон К. упоминает о безопасности потоков, которая является серьезной проблемой, если у вас есть несколько потоков, обращающихся к одному Queue
, См. http://ccutilities.codeplex.com/SourceControl/changeset/view/40529 для ThreadSafeQueue
класс, который охватывает простые вопросы безопасности потоков.
Изменить: Вот пример безопасности потока, я продолжаю указывать всем:-)
Вот пример упомянутых проблем безопасности потока. Как показано по умолчанию Queue
может "пропустить" предметы, в то же время уменьшая количество.
Обновлено: чтобы лучше представить проблему. Я никогда не добавляю пустой элемент к Queue
но стандарт Queue.Dequeue()
возвращает несколько нулевых значений. Это само по себе было бы хорошо, но при этом действительный элемент удаляется из внутренней коллекции и Count
уменьшается. В данном конкретном примере можно предположить, что каждый null
предмет, возвращенный из Queue.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
}
}
}
foreach как разумный способ перебора очереди, когда вы не удаляете элементы
Если вы хотите удалить и обработать элементы, то потокобезопасный, правильный способ - это просто удалять их по одному и обрабатывать их после их удаления.
Одним из способов является это
// 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);
}
Можно изменить количество элементов в очереди между queueList.Count и queueList.Dequeue(), поэтому для обеспечения безопасности потоков вы должны просто использовать Dequeue, но Dequeue будет выбрасывать, когда очередь пуста, поэтому вы должны использовать обработчик исключений.
// 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);
}
}
Мне кажется, вы пытаетесь обработать элемент в очереди один за другим.
Как насчет обернуть это в while
Зацикливать и обрабатывать каждый элемент из Dequeue, пока очередь не станет пустой?