/// <summary> command queue constructor. </summary> /// <param name="disposeStack"> DisposeStack. </param> /// <param name="cmdMessenger"> The command messenger. </param> public CommandQueue(DisposeStack disposeStack, CmdMessenger cmdMessenger) { CmdMessenger = cmdMessenger; disposeStack.Push(this); EventWaiter = new EventWaiter(); // Create queue thread and wait for it to start QueueThread = new Thread(ProcessQueue) { Priority = ThreadPriority.Normal }; QueueThread.Start(); while (!QueueThread.IsAlive && QueueThread.ThreadState != ThreadState.Running) { Thread.Sleep(TimeSpan.FromMilliseconds(25)); } }
/// <summary> Queue the send command wrapped in a command strategy. </summary> /// <param name="commandStrategy"> The command strategy. </param> public override void QueueCommand(CommandStrategy commandStrategy) { while (Queue.Count > MaxQueueLength) { Thread.Yield(); } lock (Queue) { // Process commandStrategy enqueue associated with command commandStrategy.CommandQueue = Queue; commandStrategy.ThreadRunState = ThreadRunState; commandStrategy.Enqueue(); // Process all generic enqueue strategies foreach (var generalStrategy in GeneralStrategies) { generalStrategy.OnEnqueue(); } } EventWaiter.Set(); }
/// <summary> Process the queue. </summary> protected override void ProcessQueue() { // Endless loop unless aborted while (ThreadRunState != ThreadRunStates.Abort) { // Calculate sleep time based on incoming command speed //_queueSpeed.SetCount(Queue.Count); //_queueSpeed.CalcSleepTime(); EventWaiter.Wait(1000); // Process queue unless stopped if (ThreadRunState == ThreadRunStates.Start) { // Only actually sleep if there are no commands in the queue SendCommandsFromQueue(); // _queueSpeed.Sleep(); } //else //{ // _queueSpeed.Sleep(); //} } }