之前的一篇文章讲述了如何通过 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. 小计:Shopee批量删除修复~附脚本

    需求 昨天浪的时候,无意之间看到文职人员在一个个删除违禁商品,大概23个店铺,每个店铺500多个商品,页面是用Ajax异步加载的,每删一个就需要等几秒,粗略估计一下用时:9h左右 然后了解了下是什么情 ...

  2. Java 8 特性 —— Stream

    Stream 是用函数式编程方式在集合类上进行复杂操作的工具,其集成了Java 8 中的众多新特性之一的聚合操作,开发者可以更容易地使用Lambda表达式,并且更方便地实现对集合的查找.遍历.过滤以及 ...

  3. vue 中的通过搜索框进行数据过滤的过程

    <template> <div> <input type="text" v-model="searchId" placeholde ...

  4. 如何安装多个mysql 或者如何更改mysql服务名

    此教程适合免安装版本(压缩包)的mysql: 有的时候你需要一台计算机上安装不同的mysql版本,而不同版本的mysql服务名称都是mysql,安装时会有冲突 解决的办法就是安装的时候更改名字 在命令 ...

  5. 多输入select

    目录 多输入select IO模型 select介绍 小demo 注意 引入电子书 title: 多输入select date: 2019/3/20 17:21:34 toc: true --- 多输 ...

  6. ES 常用java api

    java rest client 有两种: 1.Java Low Level REST Client :用于Elasticsearch的官方低层客户端.它允许通过http与Elasticsearch集 ...

  7. Hadoop集群管理

    1.简介 Hadoop是大数据通用处理平台,提供了分布式文件存储以及分布式离线并行计算,由于Hadoop的高拓展性,在使用Hadoop时通常以集群的方式运行,集群中的节点可达上千个,能够处理PB级的数 ...

  8. 一个Ajax读数据并使用IScroll显示辅助类

    花了2天时间对iscroll进行了一些封装,方便进行ajax读取数据和显示 1.IScroll直接使用的话还是挺麻烦的,特别是牵涉到分页加载动态加载数据时,以下是核心实现代码. 2.Loading提示 ...

  9. webpack打包理解

    webpack打包理解(将所有依赖文件打包到一个文件中) 由于前端代码变得越来越多,越来越复杂, 纯粹脚本化的代码书写方式已经不能满足工程化得需求. 前端模块被抽象出来, 不仅仅包括js模块, 其它如 ...

  10. 如何使用门罗币远程节点remote node?

    当使用门罗币钱包的时候,都需要启动monerod,用来同步门罗币区块. 但是因为区块体积目前已经超过40G了, 所以需要花费很多天时间才能把数据同步完. 这对于使用门罗币非常的不方便. 远程节点rem ...