最近需要定时同步数据,所以考虑使用windows服务,在晚上24点进行同步。
首先要创建一个windows组件,继承自ServiceBase,这就是windows服务的主体。
LiftService.cs 这个是服务主体类,LiftThreadWorker 是一个后台线程类对象。
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.ServiceProcess;
using System.Text;
using SDIS.Lift.Model.Business;
using SDIS.Lift.DAL.Business;
using SDIS.Lift.Model.District;
using SDIS.Lift.DAL;
using SDIS.Lift.DAL.District;
using System.Windows.Forms;
namespace LiftWinService
{
public partial class ListService : ServiceBase
{
public ListService()
{
InitializeComponent();
}
protected override void OnStart(string[] args)
{
this.LiftThreadWorker.RunWorkerAsync();
}
protected override void OnStop()
{
// TODO: 在此处添加代码以执行停止服务所需的关闭操作。
this.LiftThreadWorker.CancelAsync();
}
private void MoveDataToMirror(IList<DistrictInfo> cities, string typeid)
{
EquipmentWorker worker = new EquipmentWorker();
foreach (DistrictInfo city in cities)
{
DataSet goldDS = GetDsFromGold(city.DistrictID, typeid);
//循环插入各条记录
for (int j = 0; j < goldDS.Tables[0].Rows.Count; j++)
{
//创建mode对象
SEBASEINFO info = new SEBASEINFO();
//将表格中一行中的数据组织成Model对象
//内循环的标志位错误i->j
BaseDAL.DataTableRowToModel(goldDS.Tables[0].Rows[j], info);
//调用DAL 将Info插入到Access数据库中
try
{
//插入数据到镜像表
worker.AddSEBaseInfoToMirror(info, "MIRROR");
}
catch (Exception e)
{
}
}
}
}
private IList<DistrictInfo> GetCities()
{
DistrictWorker worker = new DistrictWorker();
return worker.GetSDXQList();
}
private DataSet GetDsFromGold(string zoneid, string typeid)
{
EquipmentWorker worker = new EquipmentWorker();
return worker.GetSEInfosFromGoldToDS(zoneid, typeid);
}
}
}
OnStart:服务启动时调用
OnStop:服务关闭时调用
当然,还有暂停等多个方法,具体残障msdn吧,这里这两个够用了。
这个类的Designer
namespace LiftWinService { partial class ListService { /// <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.LiftThreadWorker = new LiftThread(); this.ServiceName = "LiftService"; } #endregion private LiftThread LiftThreadWorker; } }
windows服务是不能直接运行的,需要向系统注册,这里就需要一个安装类。
ProjectInstaller.cs 继承Installer
using System; using System.Collections.Generic; using System.ComponentModel; using System.Configuration.Install; namespace LiftWinService { [RunInstaller(true)] public partial class ProjectInstaller : Installer { public ProjectInstaller() { InitializeComponent(); } } }
Designer
namespace LiftWinService { partial class ProjectInstaller { /// <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.serviceProcessInstaller1 = new System.ServiceProcess.ServiceProcessInstaller(); this.serviceInstaller1 = new System.ServiceProcess.ServiceInstaller(); // // serviceProcessInstaller1 // this.serviceProcessInstaller1.Account = System.ServiceProcess.ServiceAccount.LocalSystem; this.serviceProcessInstaller1.Password = null; this.serviceProcessInstaller1.Username = null; // // serviceInstaller1 // this.serviceInstaller1.Description = "电梯维保设备信息同步服务"; this.serviceInstaller1.ServiceName = "LiftService"; this.serviceInstaller1.StartType = System.ServiceProcess.ServiceStartMode.Automatic; // // ProjectInstaller // this.Installers.AddRange(new System.Configuration.Install.Installer[] { this.serviceProcessInstaller1, this.serviceInstaller1}); } #endregion private System.ServiceProcess.ServiceProcessInstaller serviceProcessInstaller1; private System.ServiceProcess.ServiceInstaller serviceInstaller1; } }
InitializeComponent中是一些参数的设定,根据情况,一般只需要修改下Description与ServiceName即可
Description:服务具体描述ServiceName:向系统注册的服务的名称
StartType:服务启动方式,这里是自动
里面还有2个Installer 一般不需要修改,用来想系统注册服务。
这样服务就开发工作就完成了。
但是,编译后不能运行对吧,下面说一下安装与卸载方法。
我因为自己用吗,没有打成安装包,用了最简单的方式。
到System32下,把InstallUtil.exe与sc.exe复制到了debug目录下
然后在cmd里面,进入debug目录。
安装:InstallUtil LiftWinService.exe
系统会自动安装服务,注意看输出内容,提示安装成功就大功告成了。
卸载:sc delete LiftService(Installer里面定义的服务名称)
同样提示成功就完成卸载了。
使用方法不用我说了吧。
控制面板->管理工具->服务
里面找到生成的服务项目,这里是 LiftService
右键,启动。
好了,到此大功告成。