参考文章: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. asp.net 类库中获取session c#类中获取session

    asp.net  类库中获取session c#类中获取session 1. 先引入命名空间 using System.Web; using System.Web.SessionState; 在使用H ...

  2. Delphi_OD_代码_调试_Delphi反调试技术(以OD为例附核心原代码)

    1.程序窗口[chuang kou]句柄[ju bing]检测原理:用FindWindow函数[han shu]查找[cha zhao]具有相同窗口[chuang kou]类名和标题的窗口[chuan ...

  3. c++virtual inline 是否冲突

    关于inline关键字:effective c++ item33:明智运用inlining.说到:inline指令就像register指令一样,只是对编译器的一种提示,而不是一个强制命令,意思是编译器 ...

  4. wcf 请考虑增加操作超时

    发送到 net.tcp://192.168.1.18:8732/CallbackService 的请求操作在配置的超时(00:00:59.9879994)内未收到回复. 分配给此操作的时间可能已经是更 ...

  5. [LeetCode#116]Fraction to Recurring Decimal

    Problem: Given two integers representing the numerator and denominator of a fraction, return the fra ...

  6. 有关Spring Batch

    在使用Spring Batch时,在无法实现StepListener的情况下,如何使用ExecutionContext呢. 解决办法,使用宣言@BeforeStep或@AfterStep.

  7. 新图形API为unity5 带来了什么&下一代新图形API的好处

    西瓜的演讲ppt翻译+解释+其他: wolf96 在最基本的层面上,这些新api是为了改进CPU性能和效率,通过:减少CPU渲染瓶颈的情况,提供更多可预测和稳定的驱动的行为,给应用程序更多控制,就像在 ...

  8. java基础(七)面向对象(二)

    这里有我之前上课总结的一些知识点以及代码大部分是老师讲的笔记 个人认为是非常好的,,也是比较经典的内容,真诚的希望这些对于那些想学习的人有所帮助! 由于代码是分模块的上传非常的不便.也比较多,讲的也是 ...

  9. 【索引】UML学习笔记

    行为图 交互图 交互概览图 时间图 顺序图 通信图 活动图 状态及图 用例图 结构图 包图 类图 对象图 组件图 部署图 组合结构图

  10. bzoj 1188 [HNOI2007]分裂游戏(SG函数,博弈)

    1188: [HNOI2007]分裂游戏 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 733  Solved: 451[Submit][Status ...