Example #1
0
        void _Worker()
        {
            try
            {
                while (!m_PendingStop)
                {
                    m_NewItemSignal.WaitOne();

                    PoolQueueItem item = null;
                    lock (m_ItemQueue)
                    {
                        if (m_ItemQueue.Count > 0)
                        {
                            item = m_ItemQueue.Dequeue();
                        }

                        // 如果队列中还有其它工作项,则再触发之
                        if (m_ItemQueue.Count > 0)
                        {
                            m_NewItemSignal.Set();
                        }
                    }

                    if (m_PendingStop)
                    {
                        break;
                    }

                    if (item != null)
                    {
                        item.Status.Status = ThreadState.Running;

                        try
                        {
                            item.Item(item.Parameter);
                            item.Status.Status = ThreadState.Stopped;
                        }
                        catch (ThreadAbortException ex)
                        {
                            item.Status.Status = ThreadState.Aborted;
                            Util.Logger.Instance().SendLog("ThreadPool", "Somebody try to abort current thread.");
                        }
                        catch (Exception ex)
                        {
                            Util.Logger.Instance().SendLog("ThreadPool", "Unhandled exception occurred: " + Environment.NewLine + ex.ToString());
                            item.Status.Status = ThreadState.Stopped;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Util.Logger.Instance().SendLog("ThreadPool", ex.ToString());
            }
            finally
            {
                Util.Logger.Instance().SendLog("ThreadPool", string.Format("Thread[{0}] ended.", Thread.CurrentThread.ManagedThreadId));
            }
        }
Example #2
0
        void _Worker()
        {
            int current_thr_id = Thread.CurrentThread.ManagedThreadId;

            try
            {
                while (!m_PendingStop)
                {
                    m_NewItemSignal.WaitOne();

                    // 从任务队列中取出等待的任务,放入本线程执行
                    PoolQueueItem item = null;
                    lock (m_ItemQueue)
                    {
                        if (m_ItemQueue.Count > 0)
                        {
                            item = m_ItemQueue.Dequeue();
                        }

                        //
                        if (m_ItemQueue.Count > 0)
                        {
                            m_NewItemSignal.Set();
                        }
                    }

                    if (m_PendingStop)
                    {
                        break;
                    }

                    if (item != null)
                    {
                        // 查找线程池中本线程的相关配置信息
                        ThreadInfo info = null;
                        lock (m_Pool)
                        {
                            info = m_Pool.Find(
                                delegate(ThreadInfo t)
                            {
                                return(t._ThrID == current_thr_id);
                            });
                        }

                        if (info != null)
                        {
                            info._Timeout = item.Timeout;
                            info._TimeMeter.Reset();
                        }
                        else
                        {
                            throw new Exception("Cannot find thread info.");
                        }

                        item.Status.Status = System.Threading.ThreadState.Running;

                        try
                        {
                            // 开始计时,执行任务
                            info._TimeMeter.Start();
                            item.Item(item.Parameter);
                            item.Status.Status = System.Threading.ThreadState.Stopped;
                            info._TimeMeter.Stop();

                            Util.Logger.Instance().SendLog("ThreadPool", string.Format("Thread[{0}] finish job normally.{1}", Thread.CurrentThread.ManagedThreadId, Environment.NewLine));
                        }
                        catch (ThreadAbortException)
                        {
                            item.Status.Status = System.Threading.ThreadState.Aborted;

                            Util.Logger.Instance().SendLog("ThreadPool", string.Format("Someone abort the work thread[{0}].{1}", Thread.CurrentThread.ManagedThreadId, Environment.NewLine));

                            // 当调用 Abort 以终止线程时,系统将引发 ThreadAbortException。
                            // ThreadAbortException 是一个可由应用程序代码捕获的特殊异常,但在 catch 块的结尾将被再次引发,
                            // 除非调用 ResetAbort
                            // 如此之后,本线程仍可继续使用
                            Thread.ResetAbort();
                        }
                        catch (Exception ex)
                        {
                            Util.Logger.Instance().SendLog("ThreadPool", string.Format("Unhandled exception occurred[{0}]:{1}{2}", Thread.CurrentThread.ManagedThreadId, Environment.NewLine, ex.ToString()));
                            item.Status.Status = System.Threading.ThreadState.Stopped;
                        }
                        finally
                        {
                            lock (m_Pool)
                            {
                                if (info != null)
                                {
                                    info._Timeout = Timeout.Infinite;
                                    info._TimeMeter.Reset();
                                }
                            }
                        }
                    }
                } // end while
            }
            catch (ThreadAbortException ex)
            {
                #region 此部分已经不会触发
                try
                {
                    // 收到线程强制退出异常,处理线程池,删除相关信息
                    Util.Logger.Instance().SendLog("ThreadPool", string.Format("Someone abort the work thread[{0}].{1}", Thread.CurrentThread.ManagedThreadId, Environment.NewLine));

                    lock (m_Pool)
                    {
                        ThreadInfo abort = m_Pool.Find(
                            delegate(ThreadInfo t)
                        {
                            return(t._ThrID == current_thr_id);
                        });

                        if (!m_Pool.Remove(abort))
                        {
                            Util.Logger.Instance().SendLog("ThreadPool", string.Format("Thread {0} removation failed.", abort._ThrID));
                        }

                        // 重新加入一个活动线程
                        Thread th = new Thread(new ThreadStart(_Worker));
                        m_Pool.Add(new ThreadInfo(th, -1));

                        th.Start();

                        // 当调用 Abort 以终止线程时,系统将引发 ThreadAbortException。
                        // ThreadAbortException 是一个可由应用程序代码捕获的特殊异常,但在 catch 块的结尾将被再次引发,
                        // 除非调用 ResetAbort
                        Thread.ResetAbort();
                    }
                }
                catch (Exception e)
                {
                    Util.Logger.Instance().SendLog("ThreadPool", string.Format("Unhandled exception occurred[{0}] when dealing abortion:{1}{2}", Thread.CurrentThread.ManagedThreadId, Environment.NewLine, e.ToString()));
                }
                finally
                {
                }
                #endregion
            }
            catch (Exception ex)
            {
                Util.Logger.Instance().SendLog("ThreadPool", string.Format("Unhandled exception occurred[{0}]:{1}{2}", Thread.CurrentThread.ManagedThreadId, Environment.NewLine, ex.ToString()));
            }
            finally
            {
                Util.Logger.Instance().SendLog("ThreadPool", string.Format("Thread[{0}] ended.", current_thr_id));
            }
        }