/// <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);

			base.Enqueue(job);
		}
        /// <summary>
        /// Cancels a job if it has not been started already
        /// </summary>
        /// <param name="job"></param>
        public virtual bool CancelJob(BackgroundThreadPoolJob job)
        {
            // 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)
		{
			IEnumerator it = base.GetEnumerator();
			while(it.MoveNext())
				if (Guid.Equals(((BackgroundThreadPoolJob)it.Current).Id, job.Id))
					return true;
			return false;
		}	
        /// <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);
            }

            base.Enqueue(job);
        }
 /// <summary>
 /// Enqueues a thread pool job to be executed by the thread pool
 /// </summary>
 /// <param name="job"></param>
 public virtual void QueueJob(BackgroundThreadPoolJob job)
 {
     // lock the queue
     lock (this.JobQueue.SyncRoot)
     {
         // and enqueue the job to be processed
         this.JobQueue.Enqueue(job);
     }
 }
        /// <summary>
        /// Determines if the job exists in the queue
        /// </summary>
        /// <param name="job"></param>
        /// <returns></returns>
        public bool Contains(BackgroundThreadPoolJob job)
        {
            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 BackgroundThreadPoolThread class
		/// </summary>
		/// <param name="job"></param>
		public BackgroundThreadPoolThread(BackgroundThreadPoolJob job) : base()
		{
			_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>
        /// 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 virtual 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>
        /// 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);
            }
        }
Beispiel #10
0
        /// <summary>
        /// Initializes a new instance of the BackgroundThreadPoolThread class
        /// </summary>
        /// <param name="job"></param>
        public BackgroundThreadPoolThread(BackgroundThreadPoolJob job) : base()
        {
            _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>
		/// Initializes a new instance of the X class
		/// </summary>
		/// <param name="job"></param>
		public 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 BackgroundThreadPoolJobListViewItem class
        /// </summary>
        /// <param name="job"></param>
        public BackgroundThreadPoolJobListViewItem(BackgroundThreadPoolJob job) : base(job.Name)
        {
            _job = job;

            base.SubItems.Add(_job.State.ToString());
        }
 /// <summary>
 /// Initializes a new instance of the BackgroundThreadPoolJobAlreadyFinishedException class
 /// </summary>
 /// <param name="job"></param>
 public 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 BackgroundThreadPoolJobListViewItem class
		/// </summary>
		/// <param name="job"></param>
		public BackgroundThreadPoolJobListViewItem(BackgroundThreadPoolJob job) : base(job.Name)
		{
			_job = job;
				
			base.SubItems.Add(_job.State.ToString());
		}		
 /// <summary>
 /// Initializes a new instance of the X class
 /// </summary>
 /// <param name="job"></param>
 public 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>
		/// Creates a queued thread pool job that will disconnect a connection for the address book item
		/// </summary>
		/// <param name="item"></param>
		public virtual void Disconnect(AddressBookItem item)
		{
			BackgroundThreadPoolJob job = new BackgroundThreadPoolJob(
				item.Id, 
				true, 
				this, 
				new object[] {item}, 
				new BackgroundThreadStartEventHandler(OnDisconnectSessionForAddressBookItem), 
				null);

			_threadPool.QueueJob(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 virtual 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>
		/// Cancels a job if it has not been started already
		/// </summary>
		/// <param name="job"></param>
		public virtual bool CancelJob(BackgroundThreadPoolJob job)
		{
			// 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 virtual 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"></param>
		public BackgroundThreadPoolJobAlreadyFinishedException(BackgroundThreadPoolJob job) : base("The job has already finished. A job cannot be cancelled after it has already finished.")
		{
			_job = job;
		}
		/// <summary>
		/// Creates a queued thread pool job that will connect a connection for the address book item
		/// </summary>
		/// <param name="item"></param>
		/// <param name="disconnectFirst"></param>
		public virtual void Connect(AddressBookItem item, bool disconnectFirst, bool verboseSession, bool autoRecv)
		{
			BackgroundThreadPoolJob job = new BackgroundThreadPoolJob(
				item.Id, 
				true, 
				this, 
				new object[] {item, disconnectFirst, verboseSession, autoRecv}, 
				new BackgroundThreadStartEventHandler(OnConnectSessionForAddressBookItem), 
				null);

			_threadPool.QueueJob(job);
		}