/// <summary>
        /// Cancels a job if it has not been started already
        /// </summary>
        /// <param name="job"></param>
        public bool CancelJob(BackgroundThreadPoolJob job)
        {
            throw new NotImplementedException("Job cancellation is not supported at this time.");

            // first check in the queue
            // then check for a running job
//			return false;
        }
 /// <summary>
 /// Enqueues a thread pool job to be executed by the thread pool
 /// </summary>
 /// <param name="job"></param>
 public void QueueJob(BackgroundThreadPoolJob job)
 {
     // lock the queue
     lock (this.JobQueue.SyncRoot)
     {
         // and enqueue the job to be processed
         this.JobQueue.Enqueue(job);
     }
 }
        /// <summary>
        /// Adds a job to the queue
        /// </summary>
        /// <param name="job"></param>
        public void Enqueue(BackgroundThreadPoolJob job)
        {
            if (this.Contains(job))
            {
                throw new BackgroundThreadPoolJobAlreadyQueuedException(job);
            }

            lock (base.SyncRoot)
            {
                base.Enqueue(job);
            }
        }
        /// <summary>
        /// Creates a thread pool job and enqueues it for the thread pool to execute
        /// </summary>
        /// <param name="startInfo">The background thread start info that will be executed as a thread pool job</param>
        /// <returns></returns>
        public BackgroundThreadPoolJob QueueJob(string name, BackgroundThreadStartInfo startInfo)
        {
            // lock the queue
            lock (this.JobQueue.SyncRoot)
            {
                // create a new job
                BackgroundThreadPoolJob job = new BackgroundThreadPoolJob(name, startInfo);

                // and enqueue the job to be processed
                this.JobQueue.Enqueue(job);

                // return the job that was created and enqueued
                return(job);
            }
        }
 /// <summary>
 /// Determines if the job exists in the queue
 /// </summary>
 /// <param name="job"></param>
 /// <returns></returns>
 public bool Contains(BackgroundThreadPoolJob job)
 {
     lock (base.SyncRoot)
     {
         IEnumerator it = base.GetEnumerator();
         while (it.MoveNext())
         {
             if (Guid.Equals(((BackgroundThreadPoolJob)it.Current).Id, job.Id))
             {
                 return(true);
             }
         }
         return(false);
     }
 }
        /// <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);
            }
        }
		/// <summary>
		/// Initializes a new instance of the BackgroundThreadPoolThread class
		/// </summary>
		/// <param name="job"></param>
		internal BackgroundThreadPoolThread(BackgroundThreadPoolJob job) : 
			base()
		{
			if (job == null)
				throw new ArgumentNullException("job");

			_job = job;
					
			// wire the job's start info to the thread events
			base.Run += _job.StartInfo.Run;	
		
			if (_job.StartInfo.Finished != null)
				base.Finished += _job.StartInfo.Finished;

			// determine if the thread will allow ThreadAbortExceptions to be throw
            //base.AllowThreadAbortException = _job.StartInfo.AllowThreadAbortExceptions;

			// start the thread automatically
			base.Start(true, _job.StartInfo.Args);								
		}
Ejemplo n.º 8
0
        /// <summary>
        /// Initializes a new instance of the BackgroundThreadPoolThread class
        /// </summary>
        /// <param name="job"></param>
        internal BackgroundThreadPoolThread(BackgroundThreadPoolJob job) :
            base()
        {
            if (job == null)
            {
                throw new ArgumentNullException("job");
            }

            _job = job;

            // wire the job's start info to the thread events
            base.Run += _job.StartInfo.Run;

            if (_job.StartInfo.Finished != null)
            {
                base.Finished += _job.StartInfo.Finished;
            }

            // determine if the thread will allow ThreadAbortExceptions to be throw
            //base.AllowThreadAbortException = _job.StartInfo.AllowThreadAbortExceptions;

            // start the thread automatically
            base.Start(true, _job.StartInfo.Args);
        }
		/// <summary>
		/// Enqueues a thread pool job to be executed by the thread pool
		/// </summary>
		/// <param name="job"></param>
		public void QueueJob(BackgroundThreadPoolJob job)
		{
			// lock the queue
			lock (this.JobQueue.SyncRoot)
			{				
				// and enqueue the job to be processed
				this.JobQueue.Enqueue(job);
			}
		}
 /// <summary>
 /// Initializes a new instance of the BackgroundThreadPoolJobAlreadyFinishedException class
 /// </summary>
 /// <param name="job">The job that already finished</param>
 internal BackgroundThreadPoolJobAlreadyFinishedException(BackgroundThreadPoolJob job) :
     base("The job has already finished. A job cannot be cancelled after it has already finished.")
 {
     _job = job;
 }
			/// <summary>
			/// Initializes a new instance of the BackgroundThreadPoolJobAlreadyFinishedException class
			/// </summary>
			/// <param name="job">The job that already finished</param>
			internal BackgroundThreadPoolJobAlreadyFinishedException(BackgroundThreadPoolJob job) : 
				base("The job has already finished. A job cannot be cancelled after it has already finished.")
			{
				_job = job;
			}
		/// <summary>
		/// Cancels a job if it has not been started already
		/// </summary>
		/// <param name="job"></param>
		public bool CancelJob(BackgroundThreadPoolJob job)
		{
			throw new NotImplementedException("Job cancellation is not supported at this time.");

			// first check in the queue			
			// then check for a running job     
//			return false;
		}
		/// <summary>
		/// Determines if the job exists in the queue
		/// </summary>
		/// <param name="job"></param>
		/// <returns></returns>
		public bool Contains(BackgroundThreadPoolJob job)
		{
			lock (base.SyncRoot)
			{
				IEnumerator it = base.GetEnumerator();
				while(it.MoveNext())
				{
					if (Guid.Equals(((BackgroundThreadPoolJob)it.Current).Id, job.Id))
					{
						return true;
					}
				}
				return false;
			}
		}	
			/// <summary>
			/// Initializes a new instance of the BackgroundThreadPoolJobAlreadyQueuedException class
			/// </summary>
			/// <param name="job">The job that is already queued</param>
			internal BackgroundThreadPoolJobAlreadyQueuedException(BackgroundThreadPoolJob job) : 
				base("The job is already queued. A job cannot be queued more than one time if the previous queued job is still waiting to be executed.")
			{
				_job = job;
			}
 /// <summary>
 /// Initializes a new instance of the BackgroundThreadPoolJobAlreadyQueuedException class
 /// </summary>
 /// <param name="job">The job that is already queued</param>
 internal BackgroundThreadPoolJobAlreadyQueuedException(BackgroundThreadPoolJob job) :
     base("The job is already queued. A job cannot be queued more than one time if the previous queued job is still waiting to be executed.")
 {
     _job = job;
 }
		/// <summary>
		/// Adds a job to the queue
		/// </summary>
		/// <param name="job"></param>
		public void Enqueue(BackgroundThreadPoolJob job)
		{
			if (this.Contains(job))
				throw new BackgroundThreadPoolJobAlreadyQueuedException(job);

			lock (base.SyncRoot)
			{
				base.Enqueue(job);
			}
		}
		/// <summary>
		/// Creates a thread pool job and enqueues it for the thread pool to execute
		/// </summary>
		/// <param name="startInfo">The background thread start info that will be executed as a thread pool job</param>
		/// <returns></returns>
		public BackgroundThreadPoolJob QueueJob(string name, BackgroundThreadStartInfo startInfo)
		{
			// lock the queue
			lock (this.JobQueue.SyncRoot)
			{
				// create a new job
				BackgroundThreadPoolJob job = new BackgroundThreadPoolJob(name, startInfo);
				
				// and enqueue the job to be processed
				this.JobQueue.Enqueue(job);

				// return the job that was created and enqueued
				return job;
			}
		}