using System.Collections.Generic; using System.Linq; using GDNXFD.Data.Repositories; //using log4net; using System; using GDNXFD.Data; using System.Threading; using System.Configuration; using System.Collections; using GDNXFD.WcfService.RealtimeState; using log4net; namespace GDNXFD.WcfService { public class AdviceWorker { private static ILog logger = LogManager.GetLogger("AppInfoLog"); private bool isRunning = false; private bool cancelPolling = false; private int pollingInterval; //实时计算线程 private Thread RealtimeStateThread; //预测计算线程 private Thread PredictCalcThread; //风机信息线程 private Thread WindturbineInfoThread; private Thread TryMaintainWindturbineThread; public bool IsRunning { get { return isRunning; } } #region 单例模式 private AdviceWorker() { string str = ConfigurationManager.AppSettings["PollingInterval"]; if (!int.TryParse(str, out pollingInterval)) { pollingInterval = 10; } } public static AdviceWorker Instance { get { return SingletonCreator.instance; } } class SingletonCreator { internal static readonly AdviceWorker instance = new AdviceWorker(); } #endregion public void Start() { try { AdviceHistoryModelRepository.UpdateAllCalcResult(); } catch (Exception ex) { logger.Info(ex.Message); } if (isRunning) return; //首次更新风机信息数据 AdviceCache.Instance.GetWindturbinInfo(); Console.WriteLine("风机状态更新完毕"); WindturbineInfoThread = new Thread(WindturbinInfoUpdate); WindturbineInfoThread.SetApartmentState(ApartmentState.STA); WindturbineInfoThread.IsBackground = true; WindturbineInfoThread.Start(); Console.WriteLine("风机状态线程启动"); RealtimeStateThread = new Thread(RealtimeStateCalc); RealtimeStateThread.SetApartmentState(ApartmentState.STA); RealtimeStateThread.IsBackground = true; RealtimeStateThread.Start(); Console.WriteLine("风机状态计算线程启动"); PredictCalcThread = new Thread(PredictCalc); PredictCalcThread.SetApartmentState(ApartmentState.STA); PredictCalcThread.IsBackground = true; PredictCalcThread.Start(); Console.WriteLine("预测计算线程启动"); //.... TryMaintainWindturbineThread = new Thread(TryMaintainWindturbine); TryMaintainWindturbineThread.SetApartmentState(ApartmentState.STA); TryMaintainWindturbineThread.IsBackground = true; TryMaintainWindturbineThread.Name = "TryMaintainWindturbineThread"; TryMaintainWindturbineThread.Start(); Console.WriteLine("推荐风机维护线程启动"); //System.Diagnostics.Debug.WriteLine("推荐风机维护线程启动"); isRunning = true; } public void Stop() { //logger.Info("正在停止后台线程..."); cancelPolling = true; if (RealtimeStateThread != null) RealtimeStateThread.Join(); } /// /// 风机启停推荐方法 (实时状态计算) /// private void RealtimeStateCalc() { //logger.Info("后台线程开始!"); isRunning = true; while (!cancelPolling) { try { CalcStateSvc.Instance.CalcWindturbine(); } catch (Exception ex) { logger.Info(ex.Message); } finally { Thread.Sleep(pollingInterval * 1000); GC.Collect(); GC.WaitForPendingFinalizers(); GC.Collect(); //GC.GetTotalMemory(true); } } isRunning = false; cancelPolling = false; //logger.Info("后台线程停止!"); } /// /// 风机启停推荐方法 (预测计算) /// private void PredictCalc() { while (!cancelPolling) { try { //AdviceCache.Instance.GetPredictAdviceModel(); } catch (Exception ex) { // } finally { Thread.Sleep(pollingInterval * 1000); } } cancelPolling = false; } /// /// 全场无风将对待机状态的风机推荐维护 /// private void TryMaintainWindturbine() { while (!cancelPolling) { try { CalcStateSvc.Instance.TryMaintainWindturbine(); } catch (Exception ex) { logger.Info(ex.Message); D(ex.ToString()); //System.Diagnostics.Debug.WriteLine(ex.ToString()); } finally { Thread.Sleep(5 * 1000 * 60);//1000*60); GC.Collect(); GC.WaitForPendingFinalizers(); GC.Collect(); //GC.GetTotalMemory(true); } } } bool allowDebug = ConfigurationManager.AppSettings["AllowDebug"].ToUpper() == "Y"; bool allowPrint = ConfigurationManager.AppSettings["AllowPrint"].ToUpper() == "Y"; private void D(string debugText) { try { if (allowDebug) { System.Diagnostics.Debug.WriteLine(debugText); } if (allowPrint) { Console.WriteLine(debugText); } } catch (Exception ex) { //logger.Info(ex.Message); System.Diagnostics.Debug.WriteLine(ex.ToString()); } } /// /// 风机信息刷新方法 /// private void WindturbinInfoUpdate() { //logger.Info("后台线程开始!"); isRunning = true; while (!cancelPolling) { try { AdviceCache.Instance.GetWindturbinInfo(); } catch (Exception ex) { // } finally { Thread.Sleep(1000); GC.Collect(); GC.WaitForPendingFinalizers(); GC.Collect(); //GC.GetTotalMemory(true); } } isRunning = false; cancelPolling = false; } } }