コード例 #1
0
        /// <summary>
        /// 线程监控,停止自动启动
        /// </summary>
        private static void ThreadMonitor()
        {
            while (true)
            {
                Thread.Sleep(10000);
                try
                {
                    var r = ThreadInfos.Count(a => a.Thread != null && a.Thread.IsAlive);
                    var e = ThreadInfos.Count(a => a.Thread == null || !a.Thread.IsAlive);
                    if (e > 0)
                    {
                        var es             = ThreadInfos.Where(a => a.Thread == null || !a.Thread.IsAlive).ToArray();
                        var errThreadNames = es.Select(a => a.ThreadName).ToArray();

                        Log.Warn(string.Format("线程共{0}个,正常{1}个,异常{2}个,异常线程:{3}", ThreadInfos.Count, r, e, StrHelper.Join(errThreadNames, ",")));
                        Log.Warn(string.Format("正在重启线程{0}", StrHelper.Join(errThreadNames, ",")));

                        foreach (var thread in es)
                        {
                            var ti = ThreadInfos[ThreadInfos.FindIndex(a => a == thread)];
                            if (ti.IsParameterized)
                            {
                                ti.Thread = new Thread(p =>
                                {
                                    while (true)
                                    {
                                        try
                                        {
                                            ti.ParameterizedThreadStart(p);
                                            Log.Monitor(string.Format("线程{0}({1})执行正常", ti.ThreadName, p));
                                        }
                                        catch (Exception ex)
                                        {
                                            Log.Error(string.Format("线程{0}({1})执行异常:{2} {3}", ti.ThreadName, p, ex.Message, ex.StackTrace));
                                        }
                                        if (ti.WaitSeconds > 0)
                                        {
                                            Thread.Sleep(1000 * ti.WaitSeconds);
                                        }
                                        else
                                        {
                                            break;
                                        }
                                    }
                                });
                                ti.Thread.Start(ti.Parameter);
                            }
                            else
                            {
                                ti.Thread = new Thread(() =>
                                {
                                    while (true)
                                    {
                                        try
                                        {
                                            ti.ThreadStart();
                                            Log.Info(string.Format("线程{0}执行正常", ti.ThreadName));
                                        }
                                        catch (Exception ex)
                                        {
                                            Log.Error(string.Format("线程{0}执行异常:{1} {2}", ti.ThreadName, ex.Message, ex.StackTrace));
                                        }
                                        if (ti.WaitSeconds > 0)
                                        {
                                            Thread.Sleep(1000 * ti.WaitSeconds);
                                        }
                                        else
                                        {
                                            break;
                                        }
                                    }
                                });
                                ti.Thread.Start();
                            }
                            Thread.Sleep(1000);
                        }
                    }
                }
                catch (Exception e)
                {
                    Log.Error(e.Message + e.StackTrace);
                }
            }
        }
コード例 #2
0
 /// <summary>
 /// 转换为用分隔符连接的字符串
 /// </summary>
 /// <typeparam name="T">集合元素类型</typeparam>
 /// <param name="list">集合</param>
 /// <param name="quotes">引号,默认不带引号,范例:单引号"'"</param>
 /// <param name="separator">分隔符,默认使用逗号分隔</param>
 /// <returns></returns>
 public static string Join <T>(this IEnumerable <T> list, string quotes = "", string separator = ",")
 {
     return(StrHelper.Join(list, quotes, separator));
 }