之前的一篇文章讲述了如何通过 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. jmeter笔记(7)--参数化--用户定义的变量

    录制的脚本里面有很多的相同的数据的时候,比如服务器ip,端口号等,当更换服务器的时候,就需要手动的修改脚本里面对应的服务器ip和端口号,比较繁琐,jmeter里面有一个用户自定义变量能很好的解决这个问 ...

  2. MySQL 主从复制实战解析

    前言:前面几篇文章讲解了在应用层读写分离的配置和使用,这篇文章将来个主从复制的实战解析. 说明:主从复制,读写分离结构图 原理图 主库生成一个线程: Binlog Dump线程 1.此线程运行在主库, ...

  3. linux 常用命令集锦

    喝断片儿了,我是谁?我在什么地方?我做过些什么事?查看当前用户 who am i查看当前路径 pwd查看历史记录 history 我忘了程序放哪了,就记得个名.更新系统数据库 updatedb查找文件 ...

  4. netty的基本介绍

    一.什么是netty?为什么要用netty netty是jboss提供的一个java开源框架,netty提供异步的.事件驱动的网络应用程序框架和工具,用以快速开发高性能.高可用性的网络服务器和客户端程 ...

  5. 快速掌握Nginx(三) —— Nginx+Systemd托管netcore应用

    以前dotnet web应用程序开发完成后,我们都是使用IIS部署在Windows Server上,如今netcore技术发展迅速,因为其跨平台的特性,将dotnet web应用程序部署在更方便部署和 ...

  6. DirectX11--实现一个3D魔方(1)

    前言 可以说,魔方跟我的人生也有一定的联系. 在高中的学校接触到了魔方社,那时候的我虽然也能够还原魔方,可看到大神们总是可以非常快地还原,为此我也走上了学习高级公式CFOP的坑.当初学习的网站是在魔方 ...

  7. 【转】Steam 开发者收入计算

    全部说的话有点复杂,捡要点说说: 假设收入100美刀. 假设美区收入50刀,非美区(在美国以外的地区,俄罗斯,中国等等其他国家)收入50刀. 1.分给steam 30% 剩下70刀. 开发者所得美区收 ...

  8. [译]Ocelot - Rate Limiting

    原文 Ocelot支持对上游做访问限流,这样就可以保证下游不要负载太大了. 如果要启用访问限流,需要做如下配置: "RateLimitOptions": { "Clien ...

  9. How to delete VSTS Project

    Buiding is so easy , Where is deleting ? C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7 ...

  10. luogu P5324 [BJOI2019]删数

    传送门 不如先考虑暴力,能删的序列首先有\(1,2,3...n\),还有就是升序排序后从后往前放数,第\(i\)位要么放\(i\),要么放\(i+1\)位置的数,例如\(1,2,4,4,5,6,9,9 ...