之前的一篇文章讲述了如何通过 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的更多相关文章

  1. Windows Service中使用Threading.Timer需注意回收

    在Windows Service中使用Threading.Timer时需要注意回收池问题 Threading.Timer是基于线程池的,系统会对其进行垃圾回收. 当Threading.Timer定义在 ...

  2. .Net Windows Service(服务) 调试安装及System.Timers.Timer 使用

    Windows Service(服务)  是运行在后台的进程 1.VS建立 Windows 服务(.NET Framework) 2.添加Timer 双击Service1.cs可以拖控件(System ...

  3. [转]在windows service中使用timer

    本文转自:http://blog.csdn.net/sharpnessdotnet/article/details/7637180 一定要使用System.Timers.Timer timer 而不是 ...

  4. quartz.net结合Topshelf实现windows service服务托管的作业调度框架

    topshelf可以很简单方便的实现windows service服务,详见我的一篇博客的介绍 http://www.cnblogs.com/xiaopotian/articles/5428361.h ...

  5. 任务调度Quartz.Net之Windows Service

    这个应该是关于Quartz.Net使用的最后一篇文章了,之前的介绍都是基于Web的,这种实现任务调度的方式很少见,因为不管是MVC.WebApi还是WebService,它们都需要寄宿在IIS上运行, ...

  6. 震惊!Windows Service服务和定时任务框架quartz之间原来是这种关系……

    过场CG:   接到公司领导的文件指示,“小熊”需要在6月底去海外执行一个行动代号为[定时任务]的营救计划,这个计划关系到公司某个项目的生死(数据安全漏洞),作战部拟定两个作战方案: 方案一:使用务定 ...

  7. Aspnet Zero中使用Windows service (Topshelf)来承载Quartz.net任务

    Aspnet Zero使用Windows service (Topshelf)来承载Quartz.net任务 网上有很多关于如何使用Topshelf创建ABP的Quartz windows服务,但很少 ...

  8. C# 通过 Quartz .NET 实现 schedule job 的处理

    在实际项目的开发过程中,会有这样的功能需求:要求创建一些Job定时触发运行,比如进行一些数据的同步. 那么在 .Net Framework 中如何实现这个Timer Job的功能呢? 这里所讲的是借助 ...

  9. Quartz.Net在windows服务中的使用

    写在前面 这几天在弄一个项目,需要定时抓取一些数据,当时也想直接用timer算了.因为之前也弄过这样的项目,但是一想,已经用过了,再去使用同一种思路,未免太乏味了.就换了一种新玩法.这里将之前看到的一 ...

随机推荐

  1. Tomcat 配置文件 server.xml

    Tomcat隶属于Apache基金会,是开源的轻量级Web应用服务器,使用非常广泛.server.xml是Tomcat中最重要的配置文件,server.xml的每一个元素都对应了Tomcat中的一个组 ...

  2. C#使用WindowsMediaPlayer实现视频播放

    using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using Sy ...

  3. C++11 std::move和std::forward

    下文先从C++11引入的几个规则,如引用折叠.右值引用的特殊类型推断规则.static_cast的扩展功能说起,然后通过例子解析std::move和std::forward的推导解析过程,说明std: ...

  4. (二叉树 BFS) leetcode 107. Binary Tree Level Order Traversal II

    Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left ...

  5. 堆排序(heap sort)

    参考博客:http://bubkoo.com/2014/01/14/sort-algorithm/heap-sort/ 1.二叉树 二叉树的第 i 层至多有 2i-1 个结点:深度为 k 的二叉树至多 ...

  6. mp的猜猜看

    ~~~~|yjb1072452141---dc9339b4c33103abc4919375203e7a24|A1482583628---0142e0b6090b9b2838328445a79cd1b8 ...

  7. iptables 防火墙日常

    . 检查机目标机器 httpd 服务/etc/init.d/httpd status ========================================================= ...

  8. [置顶]Python开发之路

    阅读目录   第一篇:python入门 第二篇:数据类型.字符编码.文件处理 第三篇:函数 第四篇:模块与包 第五篇:常用模块 第六篇:面向对象 第七篇:面向对象高级 第八篇:异常处理 第九篇:网络编 ...

  9. Java中ArrayList类的用法

    1.什么是ArrayList ArrayList就是传说中的动态数组,用MSDN中的说法,就是Array的复杂版本,它提供了如下一些好处: 动态的增加和减少元素 实现了ICollection和ILis ...

  10. P5283 [十二省联考2019]异或粽子

    考场上想到了没打完,细节思路还是不是很优,我原先的想法是每一次找完后标记那个点,下次再继续找(并不是这个意思,说不清楚)但实际上和平衡树一样加个大小就很好写了 #include<bits/std ...