ASP.NET MVC 使用FluentScheduler做定时任务

源代码地址: https://github.com/fluentscheduler/FluentScheduler

使用NuGet安装FluentScheduler

这是我实际项目中用到的代码,也可看此博主的文章http://www.cnblogs.com/mafly/p/FluentScheduler.html

在Controller中写

    public class TimedTask : IJob, IRegisteredObject
    {
        private readonly object _lock = new object();

        private bool _shuttingDown;
        public Registry Start()
        {
            HostingEnvironment.RegisterObject(this);
            Registry registry = new Registry();
       //每天几点执行一次代码 registry.Schedule(()
=> Execute()).ToRunEvery(1).Days().At(01,00); return registry; } public void Execute() {lock (_lock) { if (_shuttingDown) { return; } else { //这里写每天固定时间要运行的代码 } } } public void Stop(bool immediate) { lock (_lock) { _shuttingDown = true; } HostingEnvironment.UnregisterObject(this); } }

在Global.asax中写

    public class MvcApplication : System.Web.HttpApplication
    {
        protected void Application_Start()
        {
            //定时任务
            TimedTask timedtask = new TimedTask();
            JobManager.Initialize(timedtask.Start());
        }
    }

 

posted on 2017-03-29 14:21  阿门668  阅读(2733)  评论(0编辑  收藏  举报

导航