队列应用
public class QueueFactory<T> { protected static Queue ClassQueue = new Queue(); protected T CreateRunClass(Type t) { object ObjClass = Activator.CreateInstance(t); ClassQueue.Enqueue(ObjClass); return (T)ObjClass; } public Int32 RemoveClass() { ClassQueue.Dequeue(); return ClassQueue.Count; } public Int32 GetClassRunCount(Type t) { ////75 is my print job limit. //if (ClassQueue.Count > 75) //((UserProfile)ClassQueue.Dequeue()).; //{ // //ClassQueue.Clear(); // //RedirectToAction("Index", "Home"); //} //return CreateClass(ClassName); if (ClassQueue.Count <= 3) { CreateRunClass(t); } return ClassQueue.Count; } }
调用:
UserProfile model=new UserProfile(){.....} //判断在线用户数量 if (new QueueFactory<UserProfile>().GetClassRunCount(model.GetType()) > 3) { //超出范围 } //当用户退出时,清除自己当前队列的位置 new QueueFactory<UserProfile>().RemoveClass(); return View();