Пример #1
0
        /// <summary>
        /// 归还线程
        /// </summary>
        /// <param name="thread"></param>
        private void Close(DME_Thread thread)
        {
            if (thread == null) return;
            WriteLog("归还线程" + thread.Name);

            RunningCount--;

            //看这个线程是活的还是死的,死的需要清除
            if (!thread.IsAlive)
            {
                if (Threads.Contains(thread))
                {
                    cacheLock.EnterWriteLock();
                    try
                    {
                        if (Threads.Contains(thread))
                        {
                            Threads.Remove(thread);
                            DME_Log.WriteLine("归还" + thread.Name + "时发现,线程被关闭了,设计错误!");
                        }
                    }
                    finally
                    {
                        cacheLock.ExitWriteLock();
                    }
                }
                thread.Dispose();
            }
        }
Пример #2
0
        /// <summary>
        /// 添加线程。本方法不是线程安全,调用者需要自己维护线程安全
        /// </summary>
        /// <returns></returns>
        private DME_Thread AddThread()
        {
            //保证活动线程数不超过最大线程数
            if (Threads.Count >= MaxThreads) return null;

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

            ThreadCount++;

            WriteLog("新建线程:" + thread.Name);
            return thread;
        }