创建需要计时器的windows service

1、在VS中建立windows service后,应该添加一个安装程序。

2、在默认的Service1.cs设计界面右键,添加安装程序,生成ProjectInstaller。包含两个类serviceProcessInstaller和serviceInstaller。

3、前者的属性Account一般应设为LocalSystem。

4、如需要计时器,不应使用winform的计时器,而是System.Timers.Timer。

于是在Service1.cs中

添加私有变量:

 private System.Timers.Timer timer;

然后在初始化时:

  private void InitializeComponent()
        {
            this.timer = new System.Timers.Timer();
            ((System.ComponentModel.ISupportInitialize)(this.timer)).BeginInit();
            // 
            // timer
            // 
            this.timer.Enabled = true;
            this.timer.Interval = 120000D; //循环时间,单位毫秒
            this.timer.Elapsed += new System.Timers.ElapsedEventHandler(this.aTimer_Elapsed);
            // 
            // iLearningPush
            // 
            this.ServiceName = "myService";
            ((System.ComponentModel.ISupportInitialize)(this.timer)).EndInit();

        }

 

5、Service1的属性ServiceName为部署后的服务名称

6、windows服务需要InstallUtil工具来安装。

posted @ 2014-04-01 18:34  瓜王  阅读(211)  评论(0编辑  收藏  举报