C# QuartZ使用实例写成服务
官方学习文档:http://www.quartz-scheduler.net/documentation/index.html
官方的源代码下载:http://sourceforge.net/projects/quartznet/files/quartznet/
nuget安装包地址: https://www.nuget.org/packages/Quartz/
不会写cron可以自动生成 http://cron.qqe2.com/
在程序包管理器控制台 输入Install-Package Quartz -Version 3.0.6
倒包

下面是调度的实例部分
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using Quartz;
- using Quartz.Impl;
- namespace ConsoleApp3
- {
- public class Scheduler
- {
- private static IScheduler scheduler;
- public static IScheduler Current
- {
- get
- {
- if (scheduler==null)
- {
- scheduler = StdSchedulerFactory.GetDefaultScheduler().Result;
- }
- return scheduler;
- }
- }
- /// <summary>
- /// 启动程序
- /// </summary>
- public void Start()
- {
- Current.Start();
- task();
- }
- /// <summary>
- /// 停止程序
- /// </summary>
- public void Stop()
- {
- Current.Shutdown();
- }
- /// <summary>
- /// 重启程序
- /// </summary>
- public void Restart()
- {
- Current.Shutdown();
- scheduler = null;
- Current.Start();
- }
- /// <summary>
- /// 任务
- /// </summary>
- public void task()
- {
- Job<ORders>("order", "0/1 * * * * ? ");//corn规则
- }
- /// <summary>
- /// 初始化任务
- /// </summary>
- public void Job<T>(string uid, string cronExpression)where T : IJob
- {
- //初始化创建作业
- var jobuilder = JobBuilder.Create<T>();//约束类型为 Ijob
- //根据ID 标识触发器 构建调度
- var job= jobuilder.WithIdentity(uid, uid).Build();
- var cron = (ICronTrigger)TriggerBuilder.Create() //初始化触发器
- .WithIdentity("trigger" + uid, "group" + uid)//标识触发器
- .StartNow()//触发第一次运行
- .WithCronSchedule(cronExpression)//触发规则
- .Build();//构建调度
- //将创建的类线程和规则关联
- Current.ScheduleJob(job, cron);
- }
- }
- }
要每秒执行的调度方法 Execute 去实现
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using Quartz;
- namespace ConsoleApp3
- {
- public class ORders : IJob
- {
- public Task Execute(IJobExecutionContext context)
- {
- Task task = null;
- Console.WriteLine("wewr");
- return task;
- }
- }
- }
接着使用Topshelf 创建Windows 服务
官网 http://topshelf-project.com/
git http://github.com/topshelf/Topshelf/downloads
在 nuget 搜索 Topshelf 包
在包管理工具中安装 输入Install-Package Topshelf -Version 4.0.4.161-develop
也可以 加日志 Install-Package Topshelf.Log4Net
官网给的例子 http://docs.topshelf-project.com/en/latest/configuration/quickstart.html
通过下面的实例安装服务
- using System;
- using Topshelf;
- namespace ConsoleApp3
- {
- class Program
- {
- static void Main(string[] args)
- {
- try
- {
- HostFactory.Run(x =>
- {
- x.Service<give>(s =>
- {
- s.ConstructUsing(name => new give());
- s.WhenStarted(tc => tc.Start());
- s.WhenStopped(tc => tc.Stop());
- });
- x.RunAsLocalSystem();
- x.SetDescription("测试服务");//服务描述
- x.SetDisplayName("Test.y");//显示服务名
- x.SetServiceName("Test.y");//服务名称
- });
- }
- catch (Exception ex)
- {
- Console.WriteLine(string.Format("服务启动失败,原因:{0}", ex.ToString()));
- }
- new Scheduler().Start();
- Console.ReadKey();
- }
- }
- /// <summary>
- /// 服务要跑的内容
- /// </summary>
- class give
- {
- public void Start()
- {
- try
- {
- new Scheduler().Start();//调度启动方法
- }
- catch (Exception ex)
- {
- Console.WriteLine(ex);
- }
- Console.WriteLine("执行Start方法,启动成功");
- }
- public void Stop()
- {
- }
- }
- }
下面我们制作安装服务bat 文件
- @echo off
- rem 安装Windows服务
- E:\Service\bin\Release\ConsoleApp3.exe install
- rem 启动服务
- E:\Service\bin\Release\ConsoleApp3.exe start
- @echo on
- pause
卸载服务
- @echo off
- rem 卸载Windows服务
- E:\Service\bin\Release\ConsoleApp3.exe
- uninstall @echo on pause
未完待续。。。
C# QuartZ使用实例写成服务的更多相关文章
- MongoDB 多实例安装成服务
转发自:https://www.cnblogs.com/GainLoss/p/6906937.html 1.在mongodb的官网上下载安装包 https://www.mongodb.com/down ...
- WCF 项目应用连载[3] - 双向通信 实例管理与服务端监控
WCF 项目应用连载[1] - 索引 - 轻量级的Log系统 - Lig Sample -序 第二节我们已经创建了Lig项目,并且能稳定工作了.现在我们来改进ILigAgent接口,实现WCF的双向通 ...
- 实例PK(Vue服务端渲染 VS Vue浏览器端渲染)
Vue 2.0 开始支持服务端渲染的功能,所以本文章也是基于vue 2.0以上版本.网上对于服务端渲染的资料还是比较少,最经典的莫过于Vue作者尤雨溪大神的 vue-hacker-news.本人在公司 ...
- windows下安装Redis并部署成服务
windows下安装Redis并部署成服务 Redis 是一个开源(BSD许可)的,内存中的数据结构存储系统,它可以用作数据库.缓存和消息中间件. 一:下载 下载地址: windows版本: http ...
- 不用写Windows服务实现定时器功能(FluentScheduler )
MacBook Pro 只有四个 USB Type-C 接口是否错了? 一项新技术的诞生总会对已存在的事物造成冲击或影响,如果大家都害怕冲击与影响,那这个世界永远像现在不变就行了,大家都好好的,待在自 ...
- MongoDB的安装和配置成服务的三种方法和一些难点
1. Hotfix KB2731284 or later update is not installed的问题: If you are running any edition of Windows S ...
- PHPCMS v9构建模块 - 实例之企业服务模块
下面开始第一个实例,企业服务模块,这是个比较简单的模块,做一个抛砖的作用. 模块功能分析:企业服务,企业填写招聘申请表,管理审核之后,展示作为招聘通知的功能. ■1.文件分布 modules文 ...
- 刺猬大作战(游戏引擎用Free Pascal写成,GUI用C++写成,使用SDL和Qt4)
游戏特性[编辑] 游戏引擎用Free Pascal写成,GUI用C++写成,使用SDL和Qt4[2]. 0.9.12开始支持实时动态缩放游戏画面. 个性化[编辑] 刺猬大作战有着高度定制性 游戏模式: ...
- Windows 程序注册成服务的方法
Windows 程序注册成服务的方法 将windows 程序注册成服务这个是很多后台程序需要实现的功能,注册成服务后,你的程序就可以像windows 服务一样随系统启动,并且隐藏你的控制台界面.下面介 ...
随机推荐
- C++ MFC棋牌类小游戏day2
反思了一下昨天的设计,觉得略有不足,我决定把棋盘做成单例模式.这样的话需要重新设计棋盘类,emmm,是新建棋盘类. Baord类 成员变量: Location coordinate;//棋子坐标 b ...
- 【转】Map 与 Unordered_map
map和unordered_map的差别和使用 map和unordered_map的差别还不知道或者搞不清unordered_map和map是什么的,请见:http://blog.csdn.net/b ...
- Linux使用MentoHust联网线上校园网, 回到普通有线网络却连不上?
我的解决方法如下: 在有线网的设置(Wired Settings)那里, 弹出Network窗口, 点击右下角的设置图标, 选择Security, 然后关掉802.1x Security. 然后就可以 ...
- 关于Linux MongoDB的安装
前一篇博文讲解了如何安装与配置MongoDB的windows版,本篇博文接着上一篇讲解如何在Linux系统中安装与配置MongoDB,为了演示,我问同事要了它的云服务器用于演示,当然我自己也有,但是已 ...
- Java代码审计入门篇
作者:i春秋核心白帽yanzmi 原文来自:https://bbs.ichunqiu.com/thread-42149-1-1.html 本期斗哥带来Java代码审计的一些环境和工具准备. Java这 ...
- OpenGL ES: iOS 自定义 UIView 响应屏幕旋转
iOS下使用OpenGL 如果使用GLKit View 那么不用担心屏幕旋转的问题,说明如下: If you change the size, scale factor, or drawable pr ...
- 【sping揭秘】18、使用spring访问数据
统一的数据访问异常层次体系 基于基本的jdbc封装dao层访问接口,封装不论是访问,csv文件,关系数据库(RDBMS),ladp都可以封装成一个个DAO对象来进行访问 抛出问题 可是对于我们忽略了一 ...
- HDU 4570---Multi-bit Trie(区间DP)
题目链接 Problem Description IP lookup is one of the key functions of routers for packets forwarding and ...
- 获取CPU ID--查看CPU数量/核数
Ubuntu 获取CPU序列号或者主板序列号 CPU ID 代码: sudo dmidecode -t 4 | grep ID ID: 54 06 05 00 FF FB 8B 0F 主板序列号 代码 ...
- 课程三(Structuring Machine Learning Projects),第二周(ML strategy(2)) —— 0.Learning Goals
Learning Goals Understand what multi-task learning and transfer learning are Recognize bias, varianc ...