码迷,mamicode.com
首页 > Web开发 > 详细

分享几个.NET 下的计划任务组件

时间:2014-10-27 10:37:33      阅读:406      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   color   io   os   ar   使用   sp   

Quartz

http://www.quartz-scheduler.net/(现项目在使用,可以看我之前的文章)

Hangfire

http://hangfire.io/

FluentScheduler

https://github.com/jgeurts/FluentScheduler
Nuget :Install-Package FluentScheduler
使用很简单,一直直接使用TaskManager类管理即可

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FluentScheduler;

namespace TestFluentScheduler
{
  class Program
  {
    static void Main(string[] args)
    {
      TaskManager.AddTask(() =>
      {
        //Do something...
        Console.WriteLine("Timer task,current time:{0}", DateTime.Now);
      }, t =>
      {
        //每5秒钟执行一次
        t.ToRunNow().AndEvery(5).Seconds();
        ////带有任务名称的任务定时器
        //t.WithName("TaskName").ToRunOnceAt(DateTime.Now.AddSeconds(5));
      });
      Console.ReadKey();
    }
  }
}

使用继承FluentScheduler的Registry类(需要初始化)

using FluentScheduler;

public class MyRegistry : Registry
{
    public MyRegistry()
    {
        // Schedule an ITask to run at an interval
        Schedule<MyTask>().ToRunNow().AndEvery(2).Seconds();

        // Schedule an ITask to run once, delayed by a specific time interval. 
        Schedule<MyTask>().ToRunOnceIn(5).Seconds();

        // Schedule a simple task to run at a specific time
        Schedule(() => Console.WriteLine("Timed Task - Will run every day at 9:15pm: " + DateTime.Now)).ToRunEvery(1).Days().At(21, 15);

        // Schedule a more complex action to run immediately and on an monthly interval
        Schedule(() =>
        {
            Console.WriteLine("Complex Action Task Starts: " + DateTime.Now);
            Thread.Sleep(1000);
            Console.WriteLine("Complex Action Task Ends: " + DateTime.Now);
        }).ToRunNow().AndEvery(1).Months().OnTheFirst(DayOfWeek.Monday).At(3, 0);

        //Schedule multiple tasks to be run in a single schedule
        Schedule<MyTask>().AndThen<MyOtherTask>().ToRunNow().AndEvery(5).Minutes();
    }
}

在web程序的Global.asax文件中初始化

protected void Application_Start()
{
    TaskManager.Initialize(new MyRegistry()); 
}

WebBackgrounder

http://www.nuget.org/packages/WebBackgrounder/
http://diaosbook.com/Post/2014/7/18/how-to-run-schedule-jobs-in-aspnet

Taskschedulerengine

http://taskschedulerengine.codeplex.com/

分享几个.NET 下的计划任务组件

标签:style   blog   http   color   io   os   ar   使用   sp   

原文地址:http://www.cnblogs.com/Irving/p/4053462.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!