public partial class InterfacePolicyMainServices : ServiceBase { //服务执行间隔(s) /// <summary> /// 服务执行间隔(s) /// </summary> private const int m_updateSeconds = 180; //主工作线程 /// <summary> /// 主工作线程 /// </summary> private static Thread NormalRateThread; public InterfacePolicyMainServices() { //设置主工作线程 NormalRateThread = new Thread(BuildNormalRate); } protected override void OnStart(string[] args) { try { if (!NormalRateThread.IsAlive) NormalRateThread.Start(); m_handleLog.Handle("服务已启动", "_启动"); } catch (Exception ex) { //暂停10秒后 Thread.Sleep(1000 * 10); OnStart(args); m_handleLog.Handle(ex.GetExceptionString("政策生成服务启动发生异常:"), "_异常"); } } protected override void OnStop() { try { //暂停3秒后退出 Thread.Sleep(1000 * 3); if (NormalRateThread.IsAlive) NormalRateThread.Abort(); m_handleLog.Handle("服务已停止", "_停止"); } catch (Exception ex) { //暂停10秒后 Thread.Sleep(1000 * 10); OnStop(); m_handleLog.Handle(ex.GetExceptionString("政策生成服务停止发生异常:"), "_异常"); } } /// 生成推送政策(全、增量) /// </summary> private static void BuildNormalRate() { while (true) { try { //政策生成logic } catch (Exception ex) { m_handleLog.Handle("政策生成服务(新)发生异常:" + ex.GetExceptionString()); } finally { //GC.Collect(); //每隔三分钟跑一次 Thread.Sleep(m_updateSeconds * 1000); } } }