/// <summary> /// Starts the Queue Thread. /// </summary> /// <param name="callback"></param> public void Start(ProcessThreadedQueueItem <T> callback) { // TODO if (callback == null) { Logger.LogError("Attempted to start threaded queue with null callback!"); return; } lock (this._startStopLock) { if (false == this._running) { // Create all necessary objects this._running = true; this._queue = new Queue <T>(); this._callback = callback; this._thread = new Thread(this.ThreadEntryPoint); // Set thread name this._thread.Name = "ThreadedQueue - Processing Thread: " + this._threadIdentifier; // Start thread this._thread.Start(); } else { Logger.LogWarning("Attempted to start when it is already running!"); } } }
/// <summary> /// Stops the QueueThread from processing. /// </summary> public void Stop() { lock (this._startStopLock) { // Note - HasValidFields only run if _running is true... if (this._running && this.HasValidFields()) { // Set running to false, so that the thread ends this._running = false; // Signal thread to wakeup lock (this._queueLock) { Monitor.PulseAll(this._queueLock); } // Wait for thread to end this._thread.Join(); // Clear the queue this._queue.Clear(); // Unset class fields this._thread = null; this._queue = null; this._callback = null; } else { Logger.LogWarning("Failed to stop thread: " + this._threadIdentifier); } } }
public ThreadedQueue(string threadIdentifier) { this._queueLock = new object(); this._startStopLock = new object(); this._threadIdentifier = threadIdentifier; this._thread = null; this._running = false; this._queue = null; this._callback = null; }