Ejemplo n.º 1
0
        internal string ConvStat()
        {
            if (thread.IsAlive == false)
            {
                return("dead");
            }
            System.Threading.ThreadState st = thread.ThreadState;
            if ((st & System.Threading.ThreadState.AbortRequested) != 0)
            {
                return("aborting");
            }
            if ((st & (System.Threading.ThreadState.WaitSleepJoin
                       | System.Threading.ThreadState.Stopped
                       | System.Threading.ThreadState.Suspended)) != 0)
            {
                return("sleep");
            }
            if (st == System.Threading.ThreadState.Running ||
                (st & (System.Threading.ThreadState.Background
                       | System.Threading.ThreadState.SuspendRequested
                       | System.Threading.ThreadState.StopRequested
                       | System.Threading.ThreadState.Unstarted)) != 0)
            {
                return("run");
            }

            return(String.Format("unknown({0})", st.ToString()));
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 检测日志输出
        /// </summary>
        /// <param name="isCheckRun">是否检测运行状态,false 表示输出所有线程信息</param>
        public static void CheckThreadLog(bool isCheckRun = true)
        {
            LeftArray <AutoCSer.Threading.Thread> threads = AutoCSer.Threading.Thread.GetThreads(isCheckRun).getLeftArray();

            AutoCSer.LogHelper.Debug("线程数量 " + threads.Length.toString());
            int currentId = Thread.CurrentThread.ManagedThreadId;

            foreach (AutoCSer.Threading.Thread threadInfo in threads)
            {
                Thread thread = threadInfo.Handle;
                if (thread.ManagedThreadId != currentId)
                {
                    StackTrace stack     = null;
                    Exception  exception = null;
                    System.Threading.ThreadState threadState = default(System.Threading.ThreadState);
                    bool isSuspend = false;
                    try
                    {
                        threadState = thread.ThreadState;
#pragma warning disable 618
                        if ((threadState & (System.Threading.ThreadState.StopRequested | System.Threading.ThreadState.SuspendRequested | System.Threading.ThreadState.Stopped | System.Threading.ThreadState.AbortRequested | System.Threading.ThreadState.Aborted)) == 0)
                        {
                            if ((threadState & (System.Threading.ThreadState.Unstarted | System.Threading.ThreadState.WaitSleepJoin | System.Threading.ThreadState.Suspended)) == 0)
                            {
                                thread.Suspend();
                                isSuspend = true;
                            }
                            stack = new StackTrace(thread, true);
                        }
#pragma warning restore 618
                        //if (stack.FrameCount == AutoCSer.Threading.Thread.DefaultFrameCount) stack = null;
                    }
                    catch (ThreadStateException)
                    {
                        if ((threadState & System.Threading.ThreadState.WaitSleepJoin) == 0)
                        {
                            AutoCSer.LogHelper.Debug(threadState.ToString());
                        }
                    }
                    catch (Exception error)
                    {
                        exception = error;
                    }
                    finally
                    {
#pragma warning disable 618
                        if (isSuspend)
                        {
                            thread.Resume();
                        }
#pragma warning restore 618
                    }
                    if (exception != null)
                    {
                        try
                        {
                            AutoCSer.LogHelper.Exception(exception);
                        }
                        catch { }
                    }
                    if (stack != null)
                    {
                        try
                        {
                            AutoCSer.LogHelper.Debug(stack.ToString(), LogLevel.Debug | LogLevel.AutoCSer);
                        }
                        catch { }
                    }
                }
            }
        }