/// <summary> Adds a new job to the queue. This will be picked up by the next available /// active thread. /// </summary> public void AddJob(AGIConnectionHandler runnable) { lock (jobs) { jobs.Add(runnable); Monitor.PulseAll(jobs); } }
/// <summary> /// Get a job from the pool, run it, repeat. If the obtained job is null, we exit the loop and the thread. /// </summary> public override void Run() { while (true) { AGIConnectionHandler job = ThreadPool.obtainJob(); if (job == null) { break; } job.Run(); } }
/// <summary> /// Gets a job from the queue. If none is availble the calling thread is /// blocked until one is added. /// </summary> /// <returns>the next job to service, null if the worker thread should be shut down.</returns> internal AGIConnectionHandler obtainJob() { AGIConnectionHandler job = null; lock (jobs) { while (job == null && running) { try { if (jobs.Count == 0) { Monitor.Wait(jobs); } } catch (ThreadInterruptedException ex) { #if LOGGER logger.Error("System.Threading.ThreadInterruptedException.", ex); #else throw ex; #endif } if (jobs.Count > 0) { job = jobs[0]; jobs.RemoveAt(0); } } } if (running) { return(job); } else { return(null); } }