创建一个简单的windows 服务

此服务主要用于开启服务后定时往数据库中插入数据,直到关闭服务

 1.创建一个Windows Service 项目

2.添加一个service业务功能,用一个计时器(timer)定时往数据库中插入记录

此处需要注意,在InitializeComponent方法中,需要将

 private System.Windows.Forms.Timer timer1;

this.timer1 = new System.Windows.Forms.Timer timer1();

改成

private System.Timers.Timer();

this.timer1 = new System.Timers.Timer();

否则找不到timer的Elapsed事件

3。安装服务。 在Service1.cs[Design]右击“Add Installer" 安装服务,并设置2个组件对应的属性

this.serviceProcessInstaller1.Account = System.ServiceProcess.ServiceAccount.LocalSystem;
this.serviceInstaller1.StartType = System.ServiceProcess.ServiceStartMode.Automatic;
this.serviceInstaller1.ServiceName = "MyWindowsServices";

然后安装服务,这里使用的是vs2008 Toool 命令提示:

   安装服务 installutil F:\me\MyWindowsServiceTest.exe

   卸载服务 installutil /uninstall F:\me\MyWindowsServiceTest.exe

4.安装成功后便可以再服务中查看此服务了,并且可以启动它。

 

posted @ 2011-09-06 15:39  寶貝尐膤  阅读(190)  评论(0编辑  收藏  举报