官方学习文档: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使用实例写成服务的更多相关文章

  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. UVa 11481 Arrange the Numbers (组合数学)

    题意:给定 n,m,k,问你在 1 ~ n 的排列中,前 m 个恰好有 k 个不在自己位置的排列有多少个. 析:枚举 m+1 ~ n 中有多少个恰好在自己位置,这个是C(n-m, i),然后前面选出 ...

  2. 【转】B树、B+树、B*树

    出处:http://blog.csdn.net/v_JULY_v 1.前言: 动态查找树主要有:二叉查找树(Binary Search Tree),平衡二叉查找树(Balanced Binary Se ...

  3. 125 open source Big Data architecture papers for data professionals

    https://www.linkedin.com/pulse/100-open-source-big-data-architecture-papers-anil-madan

  4. Java变成思想总结

    Java容器 collection一个独立的元素序列,这些元素都服从一条或多条规则.list必须按照插入的顺序保存元素,set不能有重复元素,queue按照排队的顺序确定对象产生的顺序.map一组成对 ...

  5. 进程池、tornado、字体

    协程:   import grequests from fake_useragent import UserAgent   urls=[f'http://bir删d.so/search?page={p ...

  6. 【C#】 使用Gsof.Native 动态调用 C动态库

    [C#] 使用Gsof.Native 动态调用 C动态库 一.背景 使用C# 开发客户端时候,我们经常会调用一些标准的动态库或是C的类库.虽然C# 提供的PInvoke的方式,但因为使用的场景的多变, ...

  7. CPP之内存分配

    new & delete expression 1. Introduction A new expression allocates and constructs an object of a ...

  8. [转]kaldi中的在线识别----Online Recognizers

    转自: http://blog.csdn.net/wbgxx333/article/details/24932533 本文是kaldi学习联盟中@冒顿翻译的,下面是@冒顿的翻译结果,在这里感谢@冒顿的 ...

  9. https网页加载http资源导致的页面报错及解决方案

    https是当下的网站的主流趋势,甚至像苹果这样的大公司,则完全要求用户必须使用https地址. 然而对于以前http链接来说,我们往往就存在一个兼容性问题,因为你不可能一下就全部切换过去,应该在很长 ...

  10. JAVA基础语法——标识符、修饰符、关键字(个人整理总结)

    JAVA基础语法——标识符.修饰符.关键字 一 . 标识符 1.1    什么是标识符 就是程序员在定义java程序时,自定义的一些名字,例如helloworld 程序里关键字class 后跟的Dem ...