예제 #1
0
파일: ThreadPoolX.cs 프로젝트: lichange/NS
        /// <summary>归还线程</summary>
        /// <param name="thread"></param>
        private void Close(ThreadX thread)
        {
            if (thread == null)
            {
                return;
            }
            WriteLog("归还线程" + thread.Name);

            RunningCount--;

            //看这个线程是活的还是死的,死的需要清除
            if (!thread.IsAlive)
            {
                if (Threads.Contains(thread))
                {
                    lock (SyncLock_Threads)
                    {
                        if (Threads.Contains(thread))
                        {
                            Threads.Remove(thread);
                            //XTrace.WriteLine("归还" + thread.Name + "时发现,线程被关闭了,设计错误!");
                        }
                    }
                }
                thread.Dispose();
            }
        }
예제 #2
0
파일: ThreadPoolX.cs 프로젝트: lichange/NS
        /// <summary>借用线程</summary>
        /// <returns></returns>
        private ThreadX Open()
        {
            lock (SyncLock_Threads)
            {
                foreach (ThreadX item in Threads)
                {
                    if (item != null && item.IsAlive && !item.Running)
                    {
                        return(item);
                    }
                }

                //没有空闲线程,加一个
                if (Threads.Count < MaxThreads)
                {
                    ThreadX thread = AddThread();
                    Threads.Add(thread);

                    RunningCount++;

                    return(thread);
                }
                else
                {
                    WriteLog("已达到最大线程数!");
                }
            }
            return(null);
        }
예제 #3
0
파일: ThreadPoolX.cs 프로젝트: lichange/NS
        void thread_OnTaskFinished(object sender, EventArgs e)
        {
            ThreadX thread = sender as ThreadX;

            ////检查事件量
            //if (thread != null && thread.Task != null && thread.Task.Event != null)
            //{
            //    thread.Task.Event.Set();
            //}

            Close(thread);

            //通知管理线程,任务完成
            Event.Set();
        }
예제 #4
0
파일: ThreadPoolX.cs 프로젝트: lichange/NS
        /// <summary>添加线程。本方法不是线程安全,调用者需要自己维护线程安全</summary>
        /// <returns></returns>
        private ThreadX AddThread()
        {
            //保证活动线程数不超过最大线程数
            if (Threads.Count >= MaxThreads)
            {
                return(null);
            }

            ThreadX thread = new ThreadX();

            //thread.Name = Name + "线程池" + ThreadCount + "号线程";
            //thread.Name = String.Format("{0}线程池{1,3}号线程", Name, ThreadCount);
            thread.Name            = Name + "P" + ThreadCount;
            thread.OnTaskFinished += new EventHandler <EventArgs>(thread_OnTaskFinished);

            ThreadCount++;

            ////暂停一下
            //Thread.Sleep(10);

            WriteLog("新建线程:" + thread.Name);
            return(thread);
        }
예제 #5
0
파일: ThreadPoolX.cs 프로젝트: lichange/NS
        /// <summary>调度包装</summary>
        private void Work()
        {
            while (true)
            {
                try
                {
                    //等待事件量,超时1秒
                    Event.WaitOne(1000, false);
                    Event.Reset();

                    lock (SyncLock_Threads)
                    {
                        #region 线程维护与统计
                        Int32 freecount = 0;
                        //清理死线程
                        for (int i = Threads.Count - 1; i >= 0; i--)
                        {
                            if (Threads[i] == null)
                            {
                                Threads.RemoveAt(i);
                                //XTrace.WriteLine(Name + "线程池的线程对象为空,设计错误!");
                            }
                            else if (!Threads[i].IsAlive)
                            {
                                Threads[i].Dispose();
                                //XTrace.WriteLine(Threads[i].Name + "处于非活动状态,设计错误!");
                                Threads.RemoveAt(i);
                            }
                            else if (!Threads[i].Running)
                            {
                                freecount++;
                            }
                        }
                        //正在处理任务的线程数
                        RunningCount = Threads.Count - freecount;

                        WriteLog("总数:" + Threads.Count + "  可用:" + freecount + " 任务数:" + Tasks.Count);

                        Int32 count = MinThreads - freecount;
                        //保留最小线程数个线程
                        if (count > 0)
                        {
                            for (int i = 0; i < count; i++)
                            {
                                ThreadX thread = AddThread();
                                if (thread != null)
                                {
                                    Threads.Add(thread);
                                }
                            }
                        }
                        else if (count < 0)//过多活动线程,清理不活跃的
                        {
                            for (int i = Threads.Count - 1; i >= 0 && count < 0; i--)
                            {
                                if (Threads[i].CanRelease)
                                {
                                    Threads[i].Dispose();
                                    Threads.RemoveAt(i);
                                    count++;
                                }
                            }
                        }
                        #endregion
                    }

                    //检查任务,分派线程
                    if (Tasks.Count > 0)
                    {
                        lock (Sync_Tasks)
                        {
                            while (Tasks.Count > 0)
                            {
                                //借一个线程
                                ThreadX thread = Open();
                                if (thread == null)
                                {
                                    break;
                                }
                                WriteLog("借得线程" + thread.Name);

                                //拿出一个任务
                                Int32 id = Tasks.Keys[0];
                                thread.Task = Tasks[id];
                                Tasks.RemoveAt(0);
                                ////设置事件量
                                //if (EnableWait)
                                //{
                                //    AutoResetEvent e = new AutoResetEvent(false);
                                //    thread.Task.Event = e;
                                //    TaskEvents.Add(thread.Task.ID, e);
                                //}

                                //处理任务
                                thread.Start();
                            }
                        }
                    }
                }
                catch (ThreadInterruptedException ex)
                {
                    LastError = ex;
                    break;
                }
                catch (ThreadAbortException ex)
                {
                    LastError = ex;

                    break;
                }
                catch (Exception ex)
                {
                    LastError = ex;
                    //XTrace.WriteException(ex);
                }
            }

            ////检查任务是否正在处理
            //if (Threads.Count > 0)
            //{
            //    lock (SyncLock_Threads)
            //    {
            //        if (Threads.Count > 0)
            //        {
            //            foreach (ThreadX item in Threads)
            //            {
            //                //取消任务线程
            //                item.Abort(false);
            //            }
            //        }
            //    }
            //}

            // 结束所有工作了,回家吧
            AbortAll();
        }