//遍历计算公式并且保存 private void CalStep() { //创建表的方法应该每次计算时都调用,而不应该只在程序启动和关闭的时候才调用!add by hlt 2014-12-8 try { TagInit.initDataBase(); } catch { } //遍历存放公式的Hashtable foreach (DictionaryEntry de in FormulaHs) { GetValueByFormule(de.Key.ToString().Trim().ToUpper()); } #region 计算完成后,将数据存入数据库中,其中包括原始点和计算点,注意存入时,将时间保持一致 for (int i = 1; i < CalDataTable.Columns.Count; i++) { //if (string.IsNullOrEmpty(CalDataTable.Rows[0][i].ToString())) //如果该列是空 //{ if (ValueHs.ContainsKey(CalDataTable.Columns[i].ColumnName.ToUpper())) { CalDataTable.Rows[0][i] = ValueHs[CalDataTable.Columns[i].ColumnName.ToUpper()].ToString(); } //} } CalcClass1.calC1();//按压力计算 if (m_IsFirstRun == false) { SQLHelper.insertDataTable(CalDataTable, ConstYXYH.CurrentTable); } #endregion }
//计算的对外方法,调用此方法,将会进行公式计算 public void CalTreeMain() { try { #region 初始化sql server TagInit.initDataBase(); dtTagsSet = TagInit.getAllData(); //dtTagsSet.Columns.Add("currentValue"); dtTagsData = SQLHelper.ExecuteDt("select * from " + ConstYXYH.CurrentTable + " where 0=1"); #endregion #region 读取公式进行计算 //StreamReader objReader = new StreamReader("formulate.txt", Encoding.Default); //string sLine = ""; //while (sLine != null) //{ // sLine = objReader.ReadLine(); // if (sLine != null && !sLine.Equals("")) // { // string lineStr = sLine.Split('%')[0].ToString(); // string[] formulaHsArr = lineStr.Split('='); // string formulaHaKey = formulaHsArr[0].ToString().Trim().ToUpper(); // if (!FormulaHs.ContainsKey(formulaHaKey)) // { // string formulaHaValue = Regex.Replace(formulaHsArr[1].ToString().Trim().ToUpper(), @"\s", ""); // FormulaHs.Add(formulaHaKey, formulaHaValue); // } // } //} //objReader.Close(); for (int i = 0; i < Formulate.formulateArr.Length; i++) { string lineStr = Formulate.formulateArr[i].Split('%')[0].ToString(); //去掉公示后面%后的注释 string[] formulaHsArr = lineStr.Split('='); //分开公式的结果和表达式 string formulaHaKey = formulaHsArr[0].ToString().Trim().ToUpper(); if (!FormulaHs.ContainsKey(formulaHaKey)) { string formulaHaValue = Regex.Replace(formulaHsArr[1].ToString().Trim().ToUpper(), @"\s", ""); FormulaHs.Add(formulaHaKey, formulaHaValue); } } #endregion //WriteLog.WriteLogs("-------"); timer1_Tick(null, null); //WriteLog.WriteLogs("11111111111111111"); //定时器要一直运行,它的定义不应该放在方法的内部,否则,运行一段时间可能就被垃圾回收而停止了;已放在了类的开头,改成了类变量,edit by hlt 2014-12-6 //System.Timers.Timer t = new System.Timers.Timer(ConstYXYH.CalIntvel); //实例化Timer类,设置间隔时间为20秒; t.Elapsed += new System.Timers.ElapsedEventHandler(timer1_Tick); //到达时间的时候执行事件; t.AutoReset = true; //设置是执行一次(false)还是一直执行(true); t.Enabled = true; //是否执行System.Timers.Timer.Elapsed事件; TimeOfTimerStart = DateTime.Now; } catch (Exception ex) { WriteLog.WriteLogs(ex.ToString()); } }
private void timer1_Tick(object sender, System.Timers.ElapsedEventArgs e) { try { if (isfinish) { isfinish = false; if (DateTime.Now.Minute < 1 && DateTime.Now.Second < 30) { dtTagsData.Dispose(); dtTagsData = null; dtTagsData = SQLHelper.ExecuteDt("select * from " + ConstYXYH.CurrentTable + " where 0=1"); //GC.Collect(); } if (PIRead.IsOperatingPI == true) { WriteLog.WriteLogs("FCZ pass"); isfinish = true; return; } dtTagsSet = TagInit.getAllData(); if (m_IsFirstRun == false) { piClass.GetPIData(dtTagsSet, dtTagsData); //后面的new datatable()替换成从pi中传入的datatable } else { DataRow rowNew = dtTagsData.NewRow(); rowNew["ValueTime"] = DateTime.Now; foreach (DataRow row in dtTagsSet.Rows) { if (row["DataSourcesNo"].ToString() == "1" && row["tag"].ToString().Trim().Length > 1) { rowNew[row["ExcelCell"].ToString()] = row["SetValue"]; } } dtTagsData.Rows.Add(rowNew); } #region if (dtTagsData.Rows.Count > ConstYXYH.AvgNum) { dtTagsData.Rows.RemoveAt(0); } //已经生成20行datatable ValueHs.Clear(); //badValueHs = new Hashtable(); //存放死循环结果的Hashtable PreCalData(dtTagsData); //预处理取数的20行数据 CalStep(); //遍历计算并存入sql server isfinish = true; #endregion if (m_IsFirstRun == true) { dtTagsData.Rows.RemoveAt(0); } m_IsFirstRun = false; } if ((DateTime.Now - timeGCCollect).TotalHours > 1) { timeGCCollect = DateTime.Now; GC.Collect(); } System.Diagnostics.Process p = System.Diagnostics.Process.GetCurrentProcess(); if (p.PagedMemorySize64 > 1000 * 1000 * 1000 & (DateTime.Now - time1GMemoryCollect).TotalMinutes > 10) { time1GMemoryCollect = DateTime.Now; WriteLog.WriteLogs("防颤振计算内存大于1G:" + p.PagedMemorySize64 + " , 程序已运行:" + (DateTime.Now - CalTree.TimeOfTimerStart).TotalHours); System.GC.Collect(); } } catch (Exception ex) { isfinish = true; WriteLog.WriteLogs(ex.ToString()); } }