官方学习文档: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

倒包

下面是调度的实例部分

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using Quartz;
  7. using Quartz.Impl;
  8.  
  9. namespace ConsoleApp3
  10. {
  11. public class Scheduler
  12. {
  13. private static IScheduler scheduler;
  14. public static IScheduler Current
  15. {
  16. get
  17. {
  18. if (scheduler==null)
  19. {
  20. scheduler = StdSchedulerFactory.GetDefaultScheduler().Result;
  21. }
  22. return scheduler;
  23. }
  24. }
  25.  
  26. /// <summary>
  27. /// 启动程序
  28. /// </summary>
  29. public void Start()
  30. {
  31. Current.Start();
  32. task();
  33. }
  34.  
  35. /// <summary>
  36. /// 停止程序
  37. /// </summary>
  38. public void Stop()
  39. {
  40. Current.Shutdown();
  41. }
  42.  
  43. /// <summary>
  44. /// 重启程序
  45. /// </summary>
  46. public void Restart()
  47. {
  48. Current.Shutdown();
  49. scheduler = null;
  50. Current.Start();
  51. }
  52.  
  53. /// <summary>
  54. /// 任务
  55. /// </summary>
  56. public void task()
  57. {
  58. Job<ORders>("order", "0/1 * * * * ? ");//corn规则
  59. }
  60.  
  61. /// <summary>
  62. /// 初始化任务
  63. /// </summary>
  64. public void Job<T>(string uid, string cronExpression)where T : IJob
  65. {
  66. //初始化创建作业
  67. var jobuilder = JobBuilder.Create<T>();//约束类型为 Ijob
  68. //根据ID 标识触发器 构建调度
  69. var job= jobuilder.WithIdentity(uid, uid).Build();
  70.  
  71. var cron = (ICronTrigger)TriggerBuilder.Create() //初始化触发器
  72. .WithIdentity("trigger" + uid, "group" + uid)//标识触发器
  73. .StartNow()//触发第一次运行
  74. .WithCronSchedule(cronExpression)//触发规则
  75. .Build();//构建调度
  76. //将创建的类线程和规则关联
  77. Current.ScheduleJob(job, cron);
  78. }
  79. }
  80. }

要每秒执行的调度方法  Execute 去实现

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using Quartz;
  7.  
  8. namespace ConsoleApp3
  9. {
  10. public class ORders : IJob
  11. {
  12. public Task Execute(IJobExecutionContext context)
  13. {
  14. Task task = null;
  15. Console.WriteLine("wewr");
  16. return task;
  17. }
  18.  
  19. }
  20. }

接着使用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

通过下面的实例安装服务

  1. using System;
  2. using Topshelf;
  3.  
  4. namespace ConsoleApp3
  5. {
  6. class Program
  7. {
  8. static void Main(string[] args)
  9. {
  10. try
  11. {
  12. HostFactory.Run(x =>
  13. {
  14. x.Service<give>(s =>
  15. {
  16. s.ConstructUsing(name => new give());
  17. s.WhenStarted(tc => tc.Start());
  18. s.WhenStopped(tc => tc.Stop());
  19.  
  20. });
  21. x.RunAsLocalSystem();
  22. x.SetDescription("测试服务");//服务描述
  23. x.SetDisplayName("Test.y");//显示服务名
  24. x.SetServiceName("Test.y");//服务名称
  25. });
  26. }
  27. catch (Exception ex)
  28. {
  29. Console.WriteLine(string.Format("服务启动失败,原因:{0}", ex.ToString()));
  30. }
  31.  
  32. new Scheduler().Start();
  33. Console.ReadKey();
  34. }
  35. }
  36.  
  37. /// <summary>
  38. /// 服务要跑的内容
  39. /// </summary>
  40. class give
  41. {
  42. public void Start()
  43. {
  44. try
  45. {
  46. new Scheduler().Start();//调度启动方法
  47. }
  48. catch (Exception ex)
  49. {
  50. Console.WriteLine(ex);
  51. }
  52. Console.WriteLine("执行Start方法,启动成功");
  53. }
  54. public void Stop()
  55. {
  56. }
  57. }
  58. }

下面我们制作安装服务bat 文件

  1. @echo off
  2.  
  3. rem 安装Windows服务
  4. E:\Service\bin\Release\ConsoleApp3.exe install
  5.  
  6. rem 启动服务
  7. E:\Service\bin\Release\ConsoleApp3.exe start
  8.  
  9. @echo on
  10.  
  11. pause

卸载服务

  1. @echo off
  2.  
  3. rem 卸载Windows服务
  1. E:\Service\bin\Release\ConsoleApp3.exe
  1. uninstall @echo on pause

未完待续。。。

C# QuartZ使用实例写成服务的更多相关文章

  1. MongoDB 多实例安装成服务

    转发自:https://www.cnblogs.com/GainLoss/p/6906937.html 1.在mongodb的官网上下载安装包 https://www.mongodb.com/down ...

  2. WCF 项目应用连载[3] - 双向通信 实例管理与服务端监控

    WCF 项目应用连载[1] - 索引 - 轻量级的Log系统 - Lig Sample -序 第二节我们已经创建了Lig项目,并且能稳定工作了.现在我们来改进ILigAgent接口,实现WCF的双向通 ...

  3. 实例PK(Vue服务端渲染 VS Vue浏览器端渲染)

    Vue 2.0 开始支持服务端渲染的功能,所以本文章也是基于vue 2.0以上版本.网上对于服务端渲染的资料还是比较少,最经典的莫过于Vue作者尤雨溪大神的 vue-hacker-news.本人在公司 ...

  4. windows下安装Redis并部署成服务

    windows下安装Redis并部署成服务 Redis 是一个开源(BSD许可)的,内存中的数据结构存储系统,它可以用作数据库.缓存和消息中间件. 一:下载 下载地址: windows版本: http ...

  5. 不用写Windows服务实现定时器功能(FluentScheduler )

    MacBook Pro 只有四个 USB Type-C 接口是否错了? 一项新技术的诞生总会对已存在的事物造成冲击或影响,如果大家都害怕冲击与影响,那这个世界永远像现在不变就行了,大家都好好的,待在自 ...

  6. MongoDB的安装和配置成服务的三种方法和一些难点

    1. Hotfix KB2731284 or later update is not installed的问题: If you are running any edition of Windows S ...

  7. PHPCMS v9构建模块 - 实例之企业服务模块

    下面开始第一个实例,企业服务模块,这是个比较简单的模块,做一个抛砖的作用.   模块功能分析:企业服务,企业填写招聘申请表,管理审核之后,展示作为招聘通知的功能.   ■1.文件分布 modules文 ...

  8. 刺猬大作战(游戏引擎用Free Pascal写成,GUI用C++写成,使用SDL和Qt4)

    游戏特性[编辑] 游戏引擎用Free Pascal写成,GUI用C++写成,使用SDL和Qt4[2]. 0.9.12开始支持实时动态缩放游戏画面. 个性化[编辑] 刺猬大作战有着高度定制性 游戏模式: ...

  9. Windows 程序注册成服务的方法

    Windows 程序注册成服务的方法 将windows 程序注册成服务这个是很多后台程序需要实现的功能,注册成服务后,你的程序就可以像windows 服务一样随系统启动,并且隐藏你的控制台界面.下面介 ...

随机推荐

  1. C++ MFC棋牌类小游戏day2

    反思了一下昨天的设计,觉得略有不足,我决定把棋盘做成单例模式.这样的话需要重新设计棋盘类,emmm,是新建棋盘类. Baord类 成员变量: Location  coordinate;//棋子坐标 b ...

  2. 【转】Map 与 Unordered_map

    map和unordered_map的差别和使用 map和unordered_map的差别还不知道或者搞不清unordered_map和map是什么的,请见:http://blog.csdn.net/b ...

  3. Linux使用MentoHust联网线上校园网, 回到普通有线网络却连不上?

    我的解决方法如下: 在有线网的设置(Wired Settings)那里, 弹出Network窗口, 点击右下角的设置图标, 选择Security, 然后关掉802.1x Security. 然后就可以 ...

  4. 关于Linux MongoDB的安装

    前一篇博文讲解了如何安装与配置MongoDB的windows版,本篇博文接着上一篇讲解如何在Linux系统中安装与配置MongoDB,为了演示,我问同事要了它的云服务器用于演示,当然我自己也有,但是已 ...

  5. Java代码审计入门篇

    作者:i春秋核心白帽yanzmi 原文来自:https://bbs.ichunqiu.com/thread-42149-1-1.html 本期斗哥带来Java代码审计的一些环境和工具准备. Java这 ...

  6. OpenGL ES: iOS 自定义 UIView 响应屏幕旋转

    iOS下使用OpenGL 如果使用GLKit View 那么不用担心屏幕旋转的问题,说明如下: If you change the size, scale factor, or drawable pr ...

  7. 【sping揭秘】18、使用spring访问数据

    统一的数据访问异常层次体系 基于基本的jdbc封装dao层访问接口,封装不论是访问,csv文件,关系数据库(RDBMS),ladp都可以封装成一个个DAO对象来进行访问 抛出问题 可是对于我们忽略了一 ...

  8. HDU 4570---Multi-bit Trie(区间DP)

    题目链接 Problem Description IP lookup is one of the key functions of routers for packets forwarding and ...

  9. 获取CPU ID--查看CPU数量/核数

    Ubuntu 获取CPU序列号或者主板序列号 CPU ID 代码: sudo dmidecode -t 4 | grep ID ID: 54 06 05 00 FF FB 8B 0F 主板序列号 代码 ...

  10. 课程三(Structuring Machine Learning Projects),第二周(ML strategy(2)) —— 0.Learning Goals

    Learning Goals Understand what multi-task learning and transfer learning are Recognize bias, varianc ...