/// <summary> /// Monitors the jobs in the job queue and delegates threads to service the waiting jobs /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected virtual void OnProcessJobs(object sender, BackgroundThreadStartEventArgs e) { try { // continuously monitor the job queue and thread count while (true) { // lock the thread list lock (this.ThreadList.SyncRoot) { // if we haven't reached the max thread limit if (this.ThreadList.Count < _maxThreads) { // lock the job queue lock (this.JobQueue.SyncRoot) { // if there are jobs waiting if (this.JobQueue.Count > 0) { // dequeue the next waiting job BackgroundThreadPoolJob job = this.JobQueue.Dequeue(); // create a new background thread pool thread to process the job BackgroundThreadPoolThread thread = new BackgroundThreadPoolThread(job); // and finally add the thread to our list of threads this.ThreadList.Add(thread); } } } this.DestroyThreads(true /* only the finished ones */); } Thread.Sleep(100); } } catch (ThreadAbortException) { // the processing thread is aborting } catch (Exception ex) { Debug.WriteLine(ex); } }
/// <summary> /// Monitors the jobs in the job queue and delegates threads to service the waiting jobs /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected virtual void OnProcessJobs(object sender, BackgroundThreadStartEventArgs e) { try { // continuously monitor the job queue and thread count while(true) { // lock the thread list lock(this.ThreadList.SyncRoot) { // if we haven't reached the max thread limit if (this.ThreadList.Count < _maxThreads) { // lock the job queue lock(this.JobQueue.SyncRoot) { // if there are jobs waiting if (this.JobQueue.Count > 0) { // dequeue the next waiting job BackgroundThreadPoolJob job = this.JobQueue.Dequeue(); // create a new background thread pool thread to process the job BackgroundThreadPoolThread thread = new BackgroundThreadPoolThread(job); // and finally add the thread to our list of threads this.ThreadList.Add(thread); } } } this.DestroyThreads(true /* only the finished ones */); } Thread.Sleep(100); } } catch(ThreadAbortException) { // the processing thread is aborting } catch(Exception ex) { Debug.WriteLine(ex); } }