C# 通过 Quartz .NET 实现Timer Job并将其注册成为Windows Service
之前的一篇文章讲述了如何通过 Quartz .NET 实现 Timer Job (http://www.cnblogs.com/mingmingruyuedlut/p/8037263.html)
在此基础上如何将实现的Timer Job注册成为Windows Service,请看如下步骤:
1):在VS中创建Windows Service的工程

2):继承 IJob 接口实现对文本文件的写值
using Quartz;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace QuartzTimerWinSerApp
{
public class EricSimpleJob : IJob
{
public Task Execute(IJobExecutionContext context)
{
string filepath = @"C:\timertest.txt"; if (!File.Exists(filepath))
{
using (FileStream fs = File.Create(filepath)) { }
} using (StreamWriter sw = new StreamWriter(filepath, true))
{
sw.WriteLine(DateTime.Now.ToLongTimeString());
} return Task.CompletedTask;
}
}
}
3):完成 IScheduler, IJobDetail 和 ITrigger 的相关配置
using Quartz;
using Quartz.Impl;
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace QuartzTimerWinSerApp
{
public class JobScheduler
{
public async void Start()
{
var props = new NameValueCollection
{
{ "quartz.serializer.type", "binary" }
};
StdSchedulerFactory schedFact = new StdSchedulerFactory(props); IScheduler sched = await schedFact.GetScheduler();
await sched.Start(); IJobDetail job = JobBuilder.Create<EricSimpleJob>()
.WithIdentity("EricJob", "EricGroup")
.Build(); ITrigger trigger = TriggerBuilder.Create()
.WithIdentity("EricTrigger", "EricGroup")
.WithSimpleSchedule(x => x.WithIntervalInSeconds().RepeatForever())
.Build(); await sched.ScheduleJob(job, trigger);
} public async void Stop()
{
IScheduler sched = await StdSchedulerFactory.GetDefaultScheduler();
await sched.Shutdown();
}
}
}
4):完成工程中Service1的处理
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Threading.Tasks; namespace QuartzTimerWinSerApp
{
public partial class Service1 : ServiceBase
{
JobScheduler scheduler;
public Service1()
{
InitializeComponent();
} protected override void OnStart(string[] args)
{
scheduler = new JobScheduler();
scheduler.Start();
} protected override void OnStop()
{
if (scheduler != null)
scheduler.Stop();
}
}
}
5):完成Program.cs中Main函数的处理
using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Threading.Tasks; namespace QuartzTimerWinSerApp
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
static void Main()
{
ServiceBase[] ServicesToRun;
ServicesToRun = new ServiceBase[]
{
new Service1()
};
ServiceBase.Run(ServicesToRun);
}
}
}
6):完成上述代码之后,build出来的exe执行文件,放到指定的目录中,然后用 .Net Framework 中的 InstallUtil.exe 完成对service的注册 (例如InstallUtil.exe为目录为:C:\Windows\Microsoft.NET\Framework64\v4.0.30319),命令行为: InstallUtil.exe ‘your .exe file path’,注:要以管理员的身份运行cmd

如果想要卸载对应的服务,那么对应的命令行为:InstallUtil.exe /u C:\CustomerWinService\....exe
7): 到Service管理界面找到刚刚安装上的Service,然后右键启动,之后就可以到对应的txt文件中看到 Timer Job 所写入的内容

更多相关内容请参考如下链接:
http://www.codingdefined.com/2016/08/schedule-tasks-as-windows-service-using.html
http://www.c-sharpcorner.com/UploadFile/8f2b4a/how-to-installuninstall-net-windows-service-C-Sharp/
C# 通过 Quartz .NET 实现Timer Job并将其注册成为Windows Service的更多相关文章
- Windows Service中使用Threading.Timer需注意回收
在Windows Service中使用Threading.Timer时需要注意回收池问题 Threading.Timer是基于线程池的,系统会对其进行垃圾回收. 当Threading.Timer定义在 ...
- .Net Windows Service(服务) 调试安装及System.Timers.Timer 使用
Windows Service(服务) 是运行在后台的进程 1.VS建立 Windows 服务(.NET Framework) 2.添加Timer 双击Service1.cs可以拖控件(System ...
- [转]在windows service中使用timer
本文转自:http://blog.csdn.net/sharpnessdotnet/article/details/7637180 一定要使用System.Timers.Timer timer 而不是 ...
- quartz.net结合Topshelf实现windows service服务托管的作业调度框架
topshelf可以很简单方便的实现windows service服务,详见我的一篇博客的介绍 http://www.cnblogs.com/xiaopotian/articles/5428361.h ...
- 任务调度Quartz.Net之Windows Service
这个应该是关于Quartz.Net使用的最后一篇文章了,之前的介绍都是基于Web的,这种实现任务调度的方式很少见,因为不管是MVC.WebApi还是WebService,它们都需要寄宿在IIS上运行, ...
- 震惊!Windows Service服务和定时任务框架quartz之间原来是这种关系……
过场CG: 接到公司领导的文件指示,“小熊”需要在6月底去海外执行一个行动代号为[定时任务]的营救计划,这个计划关系到公司某个项目的生死(数据安全漏洞),作战部拟定两个作战方案: 方案一:使用务定 ...
- Aspnet Zero中使用Windows service (Topshelf)来承载Quartz.net任务
Aspnet Zero使用Windows service (Topshelf)来承载Quartz.net任务 网上有很多关于如何使用Topshelf创建ABP的Quartz windows服务,但很少 ...
- C# 通过 Quartz .NET 实现 schedule job 的处理
在实际项目的开发过程中,会有这样的功能需求:要求创建一些Job定时触发运行,比如进行一些数据的同步. 那么在 .Net Framework 中如何实现这个Timer Job的功能呢? 这里所讲的是借助 ...
- Quartz.Net在windows服务中的使用
写在前面 这几天在弄一个项目,需要定时抓取一些数据,当时也想直接用timer算了.因为之前也弄过这样的项目,但是一想,已经用过了,再去使用同一种思路,未免太乏味了.就换了一种新玩法.这里将之前看到的一 ...
随机推荐
- Educational Codeforces Round 63 (Rated for Div. 2)
传送门 A. Reverse a Substring 题意: 给你一串 s,让你判断能否通过反转区间[l,r]的元素,使得反转后的串的字典序小于 s: 如果能,输出 "YES",并 ...
- 【转】BTree,B-Tree,B+Tree,B*Tree
B树: 即二叉搜索树: 1.所有非叶子结点至多拥有两个儿子(Left和Right): 2.所有结点存储一个关键字: 3.非叶子结点的左指针指向小于其关键字的子树,右指针指向大于其关键字的子树: 如: ...
- Web_0002:关于MongoDB的操作
1,启动moggdb服务端 打开cmd命令窗口进入到MongoDB的安装目录bin文件下: 如: cd /d F:\Program Files\mongodb\bin 执行如下命令(该命令窗口为 ...
- Python——正则表达式初步应用(一)
1.先附上转载(www.cnblogs.com/huxi)的一张图,有重要的参考价值,其含义大家请通过阅读来理解. 2.附上初步学习Python时编写的一个爬糗事百科段子的代码. # -*- codi ...
- 怎样解决canvas 插件 和html 页面中的事件冲突问题 ?
很简单 ,在html 执行事件所在的div中 设置 position:relative;
- HDU-1171 Big Event in HDU(生成函数/背包dp)
题意 给出物品种类,物品单价,每种物品的数量,尽可能把其分成价值相等的两部分. 思路 背包的思路显然是用一半总价值当作背包容量. 生成函数则是构造形如$1+x^{w[i]}+x^{2*w[i]}+.. ...
- 微软必应地图加载错误:Uncaught TypeError: Microsoft.Maps.Location is not a constructor
微软必应地图在chrome浏览器加载错误:Uncaught TypeError: Microsoft.Maps.Location is not a constructor, 原因是没有等待地图API加 ...
- 用SQL表达交并差操作
交-并-差的处理 SQL语言:并运算UNION,交运算INTERSECT,差运算EXCEPT 基本语法形式: 子查询{UNION [ALL] | INTERSECT [ALL] | EXPECT [A ...
- Selenium中三种等待的使用方式---规避网络延迟、代码不稳定问题
在UI自动化测试中,必然会遇到环境不稳定,网络慢的情况,这时如果你不做任何处理的话,代码会由于没有找到元素,而报错.这时我们就要用到wait(等待),而在Selenium中,我们可以用到一共三种等待, ...
- httpClient closeableHttpClient
https://www.cnblogs.com/lyy-2016/p/6388663.html