[Solution] Microsoft Windows 服务(3) 使用Quartz.net定时任务
Quartz是OpenSymphony开源组织在Job scheduling领域又一个开源项目,Quartz.net 就是Quartz的移植版本。Quartz可以用来创建简单或为运行十个,百个,甚至是好几万个Jobs这样复杂的程序。
官网教程:http://www.quartz-scheduler.net/documentation/index.html
优点:
1.全,启动时间,间隔,次数,结束时间都可以指定。
2.方便。不需要考虑各种线程问题了。
下载:
Install-Package Quartz
开发:
1.job 2.trigger 3.scheduler
job:一个任务项,定义要执行的事情
实现IJob接口.只有一个方法。
class SampleJob : IJob
{
public void Execute(IJobExecutionContext context)
{
File.AppendAllText("d:\\1.txt", "good");
}
}
trigger:触发器,定义何时执行多少次。
构造函数:public SimpleTriggerImpl(string name, string group, DateTimeOffset startTimeUtc, DateTimeOffset? endTimeUtc, int repeatCount, TimeSpan repeatInterval);
通过上面的ctor可以new一个调用触发器
scheduler:调度器,安排触发器和任务结合
var factory = new StdSchedulerFactory();
var sched = factory.GetScheduler();
执行:
a.把IJob包装成IJobDetail,通过public JobDetailImpl(string name, Type jobType);
b.调用IScheduler接口的调度方法:DateTimeOffset ScheduleJob(IJobDetail jobDetail, ITrigger trigger);
c.调用IScheduler接口开始方法: void Start();或者void StartDelayed(TimeSpan delay);
停止:
调用IScheduler接口开始方法: void Shutdown();
代码下载:点击下载
简单结合Quartz和TopShelf一起创建服务:
class Program
{
static void Main(string[] args)
{
HostFactory.Run(x =>
{
//要配置的服务
x.Service<RuntimeService>(c =>
{
c.ConstructUsing(name => new RuntimeService());
c.WhenStarted(s => s.Begin());
c.WhenStopped(s => s.Stop());
});
//服务的运行身份
x.RunAsLocalSystem(); x.SetDescription("服务描述");
x.SetDisplayName("显示名称");
x.SetServiceName("服务名称");
});
}
} class RealJob : IJob
{
public void Execute(IJobExecutionContext context)
{
Console.WriteLine(DateTime.Now.ToLongTimeString());
}
} class RuntimeService
{
private IScheduler sched;
public RuntimeService()
{
var factory = new StdSchedulerFactory();
sched = factory.GetScheduler();
}
public void Begin()
{
sched.ScheduleJob(new JobDetailImpl("job", typeof(RealJob)),
new SimpleTriggerImpl("trig", "group", -, new TimeSpan(, , )));
sched.Start();
} public void Stop()
{
sched.Shutdown();
}
}
[Solution] Microsoft Windows 服务(3) 使用Quartz.net定时任务的更多相关文章
- [Solution] Microsoft Windows 服务(1) C#创建Windows服务
Microsoft Windows 服务(即,以前的 NT 服务)使您能够创建在它们自己的 Windows 会话中可长时间运行的可执行应用程序.这些服务可以在计算机启动时自动启动,可以暂停和重新启动而 ...
- [Solution] Microsoft Windows 服务(2) 使用Topshelf创建Windows服务
除了通过.net提供的windows服务模板外,Topshelf是创建Windows服务的另一种方法. 官网教程:http://docs.topshelf-project.com/en/latest/ ...
- windows服务autofac注入quartz任务
一.nuget下载相关类库引用 install-package Quartz install-package Autofac install-package Autofac.Configuration ...
- 使用 Topshelf 组件一步一步创建 Windows 服务 (2) 使用Quartz.net 调度
上一篇说了如何使用 Topshelf 组件快速创建Windows服务,接下来介绍如何使用 Quartz.net 关于Quartz.net的好处,网上搜索都是一大把一大把的,我就不再多介绍. 先介绍需要 ...
- Windows服务调用Quartz.net 实现消息调度
Quartz.NET是一个开源的作业调度框架,是OpenSymphony 的 Quartz API的.NET移植,它用C#写成,可用于winform和asp.net应用中.它提供了巨大的灵活性而不牺牲 ...
- windows 服务实现定时任务调度(Quartz.Net)
我们通常在一些情况下需要软件具有一个自动执行某些任务的功能,但是又不希望直接启动软件,或者每次都要手动的来启动软件,这时我们可可以考虑到windows服务了. 首先创建一个windows服务项目(详细 ...
- C#创建、安装、卸载、调试Windows Service(Windows 服务)的简单教程
前言:Microsoft Windows 服务能够创建在它们自己的 Windows 会话中可长时间运行的可执行应用程序.这些服务可以在计算机启动时自动启动,可以暂停和重新启动而且不显示任何用户界面.这 ...
- c#写windows服务
序言 前段时间做一个数据迁移项目,刚开始用B/S架构做的项目,但B/S要寄存在IIs中,而IIs又不稳定因素,如果重启IIs就要打开页面才能运行项目.有不便之处,就改用Windows服务实现.这篇就总 ...
- C#开发Windows服务 入门
Microsoft Windows 服务(即,以前的 NT 服务)使您能够创建在它们自己的 Windows 会话中可长时间运行的可执行应用程序. 服务可以在计算机启动时自动启动,可以暂停和重新启动而且 ...
随机推荐
- Oracle Essbase入门系列(四)
成员存储类型 除了大纲计算,维度成员的另一项重要属性是存储类型,存储类型决定维度成员相关单元格的物理存储方式.在维库中编辑成员的[Data Storage]属性,下拉菜单中可选的5种,再加上Share ...
- H5长按事件
let timeOutEvent = 0 $(function () { $('#debug').on({ touchstart: function (e) { setTimeout(function ...
- C# WinForm 技巧八:界面开发之“WeifenLuo.WinFormsUI.Docking+OutLookBar” 使用
概述 最近几天一直在关注WinFrom方面的文章主要还是园子里伍华聪的博客,在看看我们自己写的项目差不忍赌啊,有想着提炼一下项目的公共部分,公共部分有分为 界面,类库两方面,今天主要是把界面 ...
- LNMP软件安装所在的目录详细
LNMP相关软件安装目录Nginx 目录: /usr/local/nginx/MySQL 目录 : /usr/local/mysql/MySQL数据库所在目录:/usr/local/mysql/var ...
- Codeforces 55D Beautiful Number (数位统计)
把数位dp写成记忆化搜索的形式,方法很赞,代码量少了很多. 下面为转载内容: a positive integer number is beautiful if and only if it is ...
- typeof instanceof 之间的区别总结
typeof 它返回值是一个字符串,该字符串说明运算数的类型. a=1; b=true; c="c"; d=function(){ console.log(" ...
- 机器学习(Machine Learning)&深度学习(Deep Learning)资料
<Brief History of Machine Learning> 介绍:这是一篇介绍机器学习历史的文章,介绍很全面,从感知机.神经网络.决策树.SVM.Adaboost到随机森林.D ...
- Spring整合JAX-WS
Jax-ws在使用上很方便,也很轻量级.重点是他是jvnet(dev.java.net)的项目,是基于java标准的(JSR181). 不过它与Spring的整合相对麻烦,于此,我将自己的一些研究结果 ...
- JIT
http://www.cppblog.com/vczh/category/9583.html
- Recover Binary Search Tree--leetcode难题讲解
Two elements of a binary search tree (BST) are swapped by mistake.Recover the tree without changing ...