我得到错误收集被修改;不能执行枚举操作。at system . collections . queue . queueenumerator . moveext ()
Queue ReqQ = (Application["ReqQ"] != null) ? ((Queue)Application["ReqQ"]) :
new Queue(50);
if (ReqQ != null)
{
foreach (object OReq in ReqQ)
{
string mId = (string)OReq;
if (mId.Split('~')[1].Equals(reqUid.Split('~')[1]) && (DateTime.Parse(mId.Split('~')[0]).AddMinutes(1 * int.Parse(string.IsNullOrEmpty(delay) ? "0" : delay)) > DateTime.Now))
{
isSuccess = false;
break;
}
}
}
else
{
ReqQ = new Queue(10);
isSuccess = true;
}
if (isSuccess)
{
if (ReqQ.Count >= 10) //only keep last 10 messages in application cache
ReqQ.Dequeue();
ReqQ.Enqueue(reqUid);
Application["ReqQ"] = ReqQ;
}
看起来您已经从多个线程(针对不同的请求)读取和修改了一个集合。首先,使用Queue
是不安全的-如果您在另一个集合中修改它的同时遍历该集合,则特别是不正确。(编辑:我刚刚注意到你甚至没有使用通用集合。如果你正在使用。net 4,没有理由使用非泛型集合…)
不清楚你想要实现什么-你可能能够摆脱仅仅改变为使用ConcurrentQueue<T>
,但你需要意识到,当你在集合上迭代的时候,你读的值可能已经在另一个线程中退出队列了。