创建项目
创建项目,在Windows下选择Windows服务(.NET Framework)。创建名称为indowsService_Test,如下图。
系统默认创建Service1.cs。
打开Serice1.cs的属性页,将ServiceName修改为ServiceTest,如下图:
添加定时器
在vs 主菜单中选“工具”项,再选“选择工具箱项”,再勾选命名空间为System.Timers的Timer,如下图:
现在在工具箱中可以看到Timer组件如下图所示。
将其拖入至Service1设计界面。将timer1的enabled 属性设置为true,Interval属性设置为1000。
双击timer1,自动生成timer1_Elapsed事件。
添加业务代码
编辑Service1.cs,引入 System.Threading命名空间并修改代码如下:
public partial classService1 :ServiceBase
{
privateThread thdStart;
privateint numTimes;
public