参考文章:http://www.cnblogs.com/jack-liang/archive/2011/05/20/2051743.html

前段时间做过一个项目,前端系统提供添加定时任务,后端系统要时刻扫描数据库中的任务并进行相关操作。对于后端系统,首先想到的就是在Window服务中创建定时任务,于是参考了网上的一些资料,顺利完成。现将创建window service的步骤记录下来,方便以后回顾查看。

1、打开VS2008/VS2010,创建window服务项目 MyWindowService。

2、添加 window 服务项 MyService.cs ,代码如下:

;

; ) ;
            ) ;

time.Interval = interval; // 设置引发 Elapsed 事件间隔时间,单位毫秒

time.AutoReset = true;

time.Enabled = false; // 指示引发 Elapsed 事件

time.Elapsed += new System.Timers.ElapsedEventHandler(time_Elapsed);

// 定时触发一个事件,如果不是定时触发,而是时刻都在运行可以使用protected override void OnStart(string[] args),和protected override void OnStop()两个方法表示启动和结束事件

time.Start();
        }

// 定时触发事件
        private void time_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            // 注意:Timer 的 Elapsed 事件每次触发都是新开一个线程
            // 若要避免多个线程同时运行可能带来的问题,可以:1、将间隔设的尽量的长;2、在方法的开头和结尾分别加上以下代码 time.Enabled = false;和time.Enabled = true;

// to do
        }

protected override void OnStart(string[] args)
        {
            // 设置引发 Elapsed 事件
            time.Enabled = true;
        }

protected override void OnStop()
        {
            // 设置取消引发 Elapsed 事件
            time.Enabled = false;
        }
    }
}

3、添加 window 服务 安装程序类 MyServiceInstaller.cs。

方法一:添加一个类 MyServiceInstaller 继承 Installer

代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceProcess;
using System.Configuration.Install;
using System.ComponentModel;

namespace SinaWeiBoService
{
    [RunInstaller(true)]
    public class MyServiceInstaller: Installer
    {
        ServiceProcessInstaller processInstall;
        ServiceInstaller serviceInstall;
        public SinaServiceInstall()
        {
            this.processInstall = new ServiceProcessInstaller();
            this.serviceInstall = new ServiceInstaller();
            // this.serviceInstall.StartType = ServiceStartMode.Automatic;

// 配置StartType 为 Automatic  (即自动启动)
            processInstall.Account = ServiceAccount.LocalSystem;
            this.serviceInstall.ServiceName = "MyService";
            this.Installers.Add(this.serviceInstall);
            this.Installers.Add(this.processInstall);

// 或使用添加集合的方法

// this.Installers.AddRange(new System.Configuration.Install.Installer[] { serviceInstall, processInstall });
        }

}
}

方法二:

打开 MyService.cs 设计视图,右键选择“添加安装程序”,程序会默认为 MyService 服务创建安装程序类。

在 InitializeComponent() 方法中进行相关设置。

4、Program.cs 文件代码如下图:

using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceProcess;
using System.Text;

namespace MyWindowService
{
    static class Program
    {
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        static void Main()
        {
            ServiceBase[] ServicesToRun;
            ServicesToRun = new ServiceBase[] 
            { 
                 new MyService() 
            };
            ServiceBase.Run(ServicesToRun);
        }
    }
}

5、项目编译成功后,打开vs命令工具,在系统中注册和卸载服务

注册服务:

C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\installutil  MyWindowService.exe

卸载服务:

C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\installutil -u MyWindowService.exe

可以将以上两行代码放在一个cmd或bat类型的文件里,这样双击就可以了。

注册后进入服务就可以看到刚才创建的服务了。

//

 

创建 window service 定时任务的更多相关文章

  1. C# window service的创建

    其实我也是第一次在博客园写博客,看到那些高手说自己要多动手写博客,于是乎自己也尝试尝试. 废话不多说.这几天在研究window service,通过查找各种大神写的博客,我终于成功的自己写出来了. 下 ...

  2. 【Window Service】关于Window Service的两三事

    引言  Window Service通常用于寄宿WCF服务或者定时作业.下面记录一下它的用法. 创建 创建Window Service项目后,可以看到Program和Service1类.Program ...

  3. window service 创建

    1:vs中创建一个 window servece 2.右键 添加安装程序 3.更改属性视图中的Account属性为LocalService(本地服务) 更改ServiceName为你自己的服务名称   ...

  4. Window Service 计划任务

    在ASP.NET中创建计划任务,就我个人而言,用过两种方式,一种是用SQL Server创建,把写好的SSIS包导入SQL Server创建的任务中,设置好时间让它去执行.还有一种就是利用window ...

  5. C# window Service实现调用有UI的应用程序(关于win xp以后的window系统)

    我开发的系统中有一接口程序(这里就称Task,是一个C#的Console Application)经常无故的死掉,导致第二天的数据不能正常解析,所以,我写了一个window service去监视Tas ...

  6. RabbitMQ.NET In Window Service

    工作中要求使用RabbitMQ,以Windows Service 模式启动,中间有遇到一些问题,网上大部分博客有误导倾向, 在这里做一个简单的记录,以免后面的人走坑: 1. 自动重新连接,不需要手动处 ...

  7. WCF宿主Window Service Demo

    尝试了下将服务寄宿在window 服务上.具体步骤如下 整个解决方案截图 一.创建window 服务 Wcf.WinService namespace Wcf.WinService { public ...

  8. 在Salesforce中创建Web Service供外部系统调用

    在Salesforce中可以创建Web Service供外部系统调用,并且可以以SOAP或者REST方式向外提供调用接口,接下来的内容将详细讲述一下用SOAP的方式创建Web Service并且用As ...

  9. zk框架中利用map类型传值来创建window,并且传值

    @Command @NotifyChange("accList") public void clear(@BindingParam("id") String a ...

随机推荐

  1. 研究OpenRisc的高人,几十篇文章

    http://blog.csdn.net/rill_zhen/article/details/8190322 流水线的实现: http://blog.csdn.net/rill_zhen/articl ...

  2. 【HDOJ】1394 Minimum Inversion Number

    逆序数的性质.1. 暴力解 #include <stdio.h> #define MAXNUM 5005 int a[MAXNUM]; int main() { int n; int i, ...

  3. 对于利用ica进行fmri激活区识别的理解

    首先,ica是一种探索性的方法,属于数据驱动的范畴. ica计算量很大,一般都是离线式计算. ica基于的猜想是,世界是加性的.在我们所研究的脑科学中,所采集到的BOLD信号,是由一些源信号所构成,更 ...

  4. Asp.Net MVC向视图View传值的三种常见的方法:

    1.通过View( parameter )的参数传递 action: public ActionResult Index() { Person person=new Person("wumi ...

  5. PHP中Content-type的MIME类型大全说明

    <?php $mimetypes = array(         'ez' => 'application/andrew-inset', 'hqx' => 'application ...

  6. hdoj 2199 Can you solve this equation?【浮点型数据二分】

    Can you solve this equation? Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K ( ...

  7. bzoj 2741 分块+可持久化trie

    多个询问l,r,求所有子区间异或和中最大是多少 强制在线 做法: 分块+可持久化trie 1.对于每块的左端点i,预处理出i到任意一个j,()i,j)间所有子区间异或和中最大为多少,复杂度O(\(n\ ...

  8. ELK Packetbeat 部署指南(15th)

    原文链接:http://www.ttlsa.com/elk/elk-packetbeat-deployment-guide/ Packetbeat 是一个实时网络数据包分析工具,与elasticsea ...

  9. SQL第二课-创建数据表

    查看有多少数据库 SHOW DATABASES; 进入数据库:USE <数据库名> 举例:USE test;//进入test数据库 查看当前进入的是哪个数据库 SELECT DATABAS ...

  10. 平时Error记录

    The Windows Firewall on this machine is currently 1.This row already belongs to another table. DataT ...