コード例 #1
0
        /// <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>
        private void OnProcessJobs(object sender, BackgroundThreadStartEventArgs e)
        {
            try
            {
                // continuously monitor the job queue and thread count
                while (true)
                {
                    // lock the thread list
                    lock (_threads.SyncRoot)
                    {
                        // if we haven't reached the max thread limit
                        if (_threads.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
                                    _threads.Add(thread);
                                }
                            }
                        }

                        this.DestroyThreads(true /* only the finished ones */);
                    }

                    Thread.Sleep(100);
                }
            }
            catch (ThreadAbortException)
            {
                // the processing thread is aborting
            }
            catch (Exception ex)
            {
                Log.WriteLine(ex);
            }
        }
コード例 #2
0
		/// <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>
		private void OnProcessJobs(object sender, BackgroundThreadStartEventArgs e)
		{
			try
			{
				// continuously monitor the job queue and thread count
				while (true)
				{		
					// lock the thread list
					lock (_threads.SyncRoot)
					{
						// if we haven't reached the max thread limit
						if (_threads.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
									_threads.Add(thread);
								}
							}
						}	
   					
						this.DestroyThreads(true /* only the finished ones */);
					}

					Thread.Sleep(100);
				}
			}
			catch(ThreadAbortException)
			{
				// the processing thread is aborting
			}
			catch(Exception ex)
			{
				Log.WriteLine(ex);
			}
		}