#region CmdMessenger - MIT - (c) 2013 Thijs Elenbaas. /* CmdMessenger - library that provides command based messaging Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. Copyright 2013 - Thijs Elenbaas */ #endregion using System; using System.Collections.Generic; namespace CommandMessenger.Queue { // Command queue base object. public abstract class CommandQueue : IDisposable { private readonly AsyncWorker _worker; protected readonly ListQueue Queue = new ListQueue(); // Buffer for commands protected readonly List GeneralStrategies = new List(); // Buffer for command independent strategies public bool IsRunning { get { return _worker.IsRunning; } } public bool IsSuspended { get { return _worker.IsSuspended; } } /// Gets count of records in queue. NOT THREAD-SAFE. public int Count { get { return Queue.Count; } } /// Gets is queue is empty. NOT THREAD-SAFE. public bool IsEmpty { get { return Queue.Count == 0; } } /// Clears the queue. public void Clear() { lock (Queue) Queue.Clear(); } protected CommandQueue() { _worker = new AsyncWorker(ProcessQueue, "CommandQueue"); } /// Adds a general strategy. This strategy is applied to all queued and dequeued commands. /// The general strategy. public void AddGeneralStrategy(GeneralStrategy generalStrategy) { // Give strategy access to queue generalStrategy.CommandQueue = Queue; // Add to general strategy list GeneralStrategies.Add(generalStrategy); } /// /// Queue the command wrapped in a command strategy. /// Call SignalWaiter method to continue processing of queue. /// /// The command strategy. public abstract void QueueCommand(CommandStrategy commandStrategy); public void Dispose() { Dispose(true); GC.SuppressFinalize(this); } public void Start() { _worker.Start(); } public void Stop() { _worker.Stop(); Clear(); } public void Suspend() { _worker.Suspend(); } public void Resume() { _worker.Resume(); } protected void SignalWorker() { _worker.Signal(); } protected virtual void Dispose(bool disposing) { if (disposing) { Stop(); } } /// Process the queue. protected abstract bool ProcessQueue(); } }