window服务程序编写及发布
log4net打印log日志必须在AssemblyInfo.cs里面增加[assembly: log4net.Config.DOMConfigurator(Watch = true)]
windows主窗体运行程序
private static readonly ILog log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
string updateTime = string.Empty;
/// <summary>
/// 上一次执行时间
/// </summary>
DateTime NewUpTime=System.DateTime.Now;
public NewOrientalDataCenterService()
{
log.Info("-------------------------日志开始输出------------------------");
InitializeComponent();
}
protected override void OnStart(string[] args)
{
MyTime.Interval = 1000;
MyTime.Start();
}
protected override void OnStop()
{
MyTime.Stop();
MyTime.Dispose();
}
private void StartGetData()
{
DayGetData getdata = new DayGetData();
getdata.GetAllData();
if (Config.IsCheck)
{
RefreshTableInfo f=new RefreshTableInfo();
f.Refresh();
}
log.Info("-------------------------此次数据已经同步完成------------------------");
}
private void TimerEventProcessor(object sender, System.Timers.ElapsedEventArgs e)
{
if (!Config.IsOpen)
{
string tim = System.DateTime.Now.ToString("H:mm");
if (Config.firstTime == tim || Config.SecondTime == tim || Config.ThirdTime == tim || Config.FourthTime == tim)
{
if (tim != updateTime)
{
MyTime.Enabled = false;
StartGetData();
MyTime.Enabled = true;
updateTime = tim;
}
}
}
else {
System.TimeSpan ND = System.DateTime.Now - NewUpTime;
if (ND.TotalMinutes >= Config.OpenTime)
{
log.Info("---------------- System.TimeSpan ND = System.DateTime.Now - NewUpTime;" + (System.DateTime.Now - NewUpTime) + "--------------");
MyTime.Enabled = false;
StartGetData();
MyTime.Enabled = true;
NewUpTime = System.DateTime.Now;
}
}
}s
窗体中增加计时器
private Timer MyTime;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
/// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region 组件设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要
/// 使用代码编辑器修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.MyTime = new System.Timers.Timer();
((System.ComponentModel.ISupportInitialize)(this.MyTime)).BeginInit();
//
// MyTime
//
this.MyTime.Elapsed += new System.Timers.ElapsedEventHandler(this.TimerEventProcessor);
//
// NewOrientalDataCenterService
//
this.ServiceName = "NewOrientalDataCenterService";
((System.ComponentModel.ISupportInitialize)(this.MyTime)).EndInit();
}s