创建一个简单的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.安装成功后便可以再服务中查看此服务了,并且可以启动它。

 

转载于:https://www.cnblogs.com/cuifuxue/archive/2011/09/06/2168823.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值