创建 window service 定时任务
参考文章: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.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.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 定时任务的更多相关文章
- C# window service的创建
其实我也是第一次在博客园写博客,看到那些高手说自己要多动手写博客,于是乎自己也尝试尝试. 废话不多说.这几天在研究window service,通过查找各种大神写的博客,我终于成功的自己写出来了. 下 ...
- 【Window Service】关于Window Service的两三事
引言 Window Service通常用于寄宿WCF服务或者定时作业.下面记录一下它的用法. 创建 创建Window Service项目后,可以看到Program和Service1类.Program ...
- window service 创建
1:vs中创建一个 window servece 2.右键 添加安装程序 3.更改属性视图中的Account属性为LocalService(本地服务) 更改ServiceName为你自己的服务名称 ...
- Window Service 计划任务
在ASP.NET中创建计划任务,就我个人而言,用过两种方式,一种是用SQL Server创建,把写好的SSIS包导入SQL Server创建的任务中,设置好时间让它去执行.还有一种就是利用window ...
- C# window Service实现调用有UI的应用程序(关于win xp以后的window系统)
我开发的系统中有一接口程序(这里就称Task,是一个C#的Console Application)经常无故的死掉,导致第二天的数据不能正常解析,所以,我写了一个window service去监视Tas ...
- RabbitMQ.NET In Window Service
工作中要求使用RabbitMQ,以Windows Service 模式启动,中间有遇到一些问题,网上大部分博客有误导倾向, 在这里做一个简单的记录,以免后面的人走坑: 1. 自动重新连接,不需要手动处 ...
- WCF宿主Window Service Demo
尝试了下将服务寄宿在window 服务上.具体步骤如下 整个解决方案截图 一.创建window 服务 Wcf.WinService namespace Wcf.WinService { public ...
- 在Salesforce中创建Web Service供外部系统调用
在Salesforce中可以创建Web Service供外部系统调用,并且可以以SOAP或者REST方式向外提供调用接口,接下来的内容将详细讲述一下用SOAP的方式创建Web Service并且用As ...
- zk框架中利用map类型传值来创建window,并且传值
@Command @NotifyChange("accList") public void clear(@BindingParam("id") String a ...
随机推荐
- c++ RTTI(runtime type info)
RTTI(Run-Time Type Information,通过运行时类型信息)程序能够使用基类的指针或引用来检查这些指针或引用所指的对象的实际派生类型. RTTI提供了以下两个非常有用的操作符: ...
- 分析WordPress主题结构是如何架构的?
利用强大的技术,可以把基于WordPress的网站做成各种各样的形式,这除了要求WordPress主题开发人员精通HTML,PHP,JS,CSS等技术,还需要开发者掌握WordPress主题的框架.下 ...
- 为xampp 安装pear db (database) 模块
pear channel-update pear.php.netpear install db
- linux 内核驱动加载过程中 向文件系统中的文件进行读写操作
utils.h 文件: #ifndef __UTILS_H__ #define __UTILS_H__ void a2f(const char *s, ...); #endif utils.c 文件: ...
- GitHub上整理的一些工具,求补充
http://segmentfault.com/q/1010000002404545 技术站点 Hacker News:非常棒的针对编程的链接聚合网站 Programming reddit:同上 MS ...
- Top 10 Java Debugging Tips with Eclipse
In this tutorial we will see about debugging java applications using Eclipse. Debugging helps us to ...
- 宁波Uber优步司机奖励政策(1月18日~1月24日)
滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...
- C# 应用正则表达式
1.检测该字符串是否 带有字符“8”(类似sql语句:select * from tableName where name like '%8%') string pattern = @"\w ...
- Bootstrap(v3.2.0)模态框(modal)垂直居中
Bootstrap(v3.2.0)模态框(modal)垂直居中方法: 在bootstrap.js文件900行后面添加如下代码,便可以实现垂直居中. that.$element.children().e ...
- Oracle keep详解
原题目 select * from dept where deptno=(select max(deptno) keep(dense_rank last order by count(1)) fr ...