using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.ServiceProcess;
using System.Text;
using WinSerCommon;
using System.Xml;
using System.Configuration;
using System.IO;
using WinCommon;
using System.Threading;
namespace WinSerMisDataSys
{
partial class MisDataSys : ServiceBase
{
public MisDataSys()
{
InitializeComponent();
}
private string strconn = ConfigurationManager.ConnectionStrings["FGSTEMPDBCONN"].ToString();
private string processcountdata = ConfigurationManager.AppSettings["processcountdata"];
private string fgscode = ConfigurationManager.AppSettings["fgscode"];
private string runtime = ConfigurationManager.AppSettings["runtime"];
private int sleeptime = 5000;
private bool isStop = false;
private Thread oThread = null;
protected override void OnStart(string[] args)
{
isStop = false;
initWebService();
// TODO: 在此处添加代码以启动服务。
//执行周期--分钟
int cycle = int.Parse(ConfigurationManager.AppSettings["cycledata"]);
sleeptime = 1000 * 60 * cycle;//时间间隔为----分钟
if (oThread != null)
{
oThread.Abort();
oThread = null;
}
if (oThread == null)
{
oThread = new Thread(new ThreadStart(syn_Data));
oThread.Name = "syndata";
oThread.Start();
}
}
protected override void OnStop()
{
isStop = true;
while (oThread.IsAlive)
{
Thread.Sleep(1000);
}
// TODO: 在此处添加代码以执行停止服务所需的关闭操作。
}
private bool isPause = false;
protected override void OnContinue()
{
isPause = false;
}
protected override void OnPause()
{
isPause = true;
}
private void initWebService()
{
try
{
string ip = ConfigurationManager.AppSettings["zgsip"];
string port = ConfigurationManager.AppSettings["zgsport"];
LHSys.InitWebService(ip, port);
}
catch (Exception e)
{
ClientLog.Add("正常数据同步(MisDataSys.initWebService)", e.Message);
}
}
private void syn_Data()
{
while (!isStop)
{
try
{
if (!isPause)
{
synBD();
}
}
catch (ThreadStateException ex)
{
ClientLog.Add("数据同步(MisDataSys.syn_Data)", ex.Message);
}
finally
{
Thread.Sleep(sleeptime);
}
}
}
/// <summary>
/// 同步数据
/// </summary>
/// <param name="source"></param>
/// <param name="e"></param>
private void synBD()
{
if (DateTime.Now.Hour.ToString() == runtime)
{
try
{
//System.Reflection.Assembly.GetExecutingAssembly().Location
//string rootPath = Directory.GetCurrentDirectory();
string rootPath = AppDomain.CurrentDomain.BaseDirectory;
XmlDocument docXml = new XmlDocument();//G:\利华燃气项目\LHRQ\WinSerMisDataSys\bin\Debug\Config
docXml.Load(rootPath + "\\Config\\Sys.xml");
//docXml.Load("G:\\利华燃气项目\\LHRQ\\WinSerMisDataSys\\bin\\Debug\\Config\\Sys.xml");
XmlNodeList tableNdList = docXml.SelectNodes("//tables/table[@issys='1']");
foreach (XmlElement el in tableNdList)
{
string tableName = el.GetAttribute("name");
string sysmode = el.GetAttribute("sysmode");
string idField = el.GetAttribute("id");
string iddel = el.GetAttribute("iddel");
if (iddel != "1")
{
//LHSys.synData(tableName, fgscode, processcountdata, strconn, sysmode);
LHSys.synDataCatch(tableName, idField, fgscode, processcountdata, strconn, sysmode);
}
}
}
catch (Exception ex)
{
ClientLog.Add("正常数据同步(MisDataSys.synBD)", ex.Message);
}
}
}
}
}