服务组件中增加时间组件如下:
private void InitializeComponent()
{
//this.components = new System.ComponentModel.Container();
//this.timer1 = new System.Windows.Forms.Timer(this.components);
this.timer1 = new System.Timers.Timer();
((System.ComponentModel.ISupportInitialize)(this.timer1)).BeginInit();
//
// timer1
//
this.timer1.Enabled = true;
this.timer1.Interval = 10000;
this.timer1.Elapsed += new System.Timers.ElapsedEventHandler(this.timer1_Elapsed);
//
// Service1
//
this.ServiceName = "Service1";
((System.ComponentModel.ISupportInitialize)(this.timer1)).EndInit();
}
#endregion
//private System.Windows.Forms.Timer timer1;
private System.Timers.Timer timer1;
在主运行中设计为:
static void Main(string[] args)
{
ServiceBase[] ServicesToRun;
// 同一进程中可以运行多个用户服务。若要将
// 另一个服务添加到此进程中,请更改下行以
// 创建另一个服务对象。例如,
//
// ServicesToRun = new ServiceBase[] {new Service1(), new MySecondUserService()};
//
ServicesToRun = new ServiceBase[] { new Service1()};
ServiceBase.Run(ServicesToRun);
}
在服务组件的界面右键选择“添加安装程序”,会增加一个ProjectInstaller组件,在此组件中右键选择属性 ,1.在serviceProcessInstaller1中Account选择
LocalService,GenerateMember选择true.2.在System.ServiceProcess.ServiceInstaller中修改NAME,DisplayName,ServiceName设备服务名,GenerateMember选择
true,StartType设置为:Automatic