Beispiel #1
0
 private void Thread_Completed(XThread thread)
 {
     lock (this.Queue)
     {
         this.Queue.Enqueue(thread);
     }
     this.StartEvent.Set();
 }
Beispiel #2
0
        /// <summary>
        /// 初始化类型
        /// </summary>
        /// <param name="count">线程池线程数</param>
        public XThreadPool(int count)
        {
            if (count < 1)
            {
                throw new ArgumentOutOfRangeException("count", "count 必需大于 0");
            }

            this.List       = new List <XThread>(count);
            this.Queue      = new Queue <XThread>(count);
            this.StartEvent = new AutoResetEvent(false);
            while (this.List.Count < count)
            {
                var thread = new XThread();
                thread.Completed += new XThreadItemCompleted(Thread_Completed);

                this.List.Add(thread);
                this.Queue.Enqueue(thread);
            }
        }
Beispiel #3
0
        /// <summary>
        /// 获取一个线程
        /// </summary>
        /// <returns></returns>
        public XThread GetThread()
        {
            XThread thread = null;

            while (true)
            {
                lock (this.Queue)
                {
                    if (this.Queue.Count > 0)
                    {
                        thread = this.Queue.Dequeue();
                        break;
                    }
                }
                this.StartEvent.WaitOne();
            }

            if (thread.Thread.ThreadState == ThreadState.Aborted || thread.Thread.ThreadState == ThreadState.Stopped)
            {
                throw new ThreadStateException(string.Format("线程状态为: {0} 不可使用。", thread.Thread.ThreadState));
            }

            return(thread);
        }