.NET 定时执行任务

13 篇文章 0 订阅
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ZK.TaskScheduling
{
    public class Initialize
    {
        /// <summary>
        /// 间隔任务
        /// </summary>
        /// <param name="interval">间隔时长</param>
        /// <param name="type">任务类型:1、TaskSchedulingType.Default(默认);2、TaskSchedulingType.Second;3、TaskSchedulingType.Minute;4、TaskSchedulingType.Hour;5、TaskSchedulingType.Day;</param>
        public static void Start(int interval, TaskSchedulingType type = TaskSchedulingType.Default)
        {
            switch (type)
            {
                case TaskSchedulingType.Default:
                    StartInterval(interval);
                    break;
                case TaskSchedulingType.Second:
                    StartIntervalSecond(interval);
                    break;
                case TaskSchedulingType.Minute:
                    StartIntervalMinute(interval);
                    break;
                case TaskSchedulingType.Hour:
                    StartIntervalHour(interval);
                    break;
                case TaskSchedulingType.Day:
                    StartIntervalDay(interval);
                    break;
                default:
                    StartInterval(interval);
                    break;
            }
        }

        /// <summary>
        /// 间隔任务(单位:毫秒)
        /// </summary>
        /// <param name="hour">间隔毫秒,默认为1毫秒</param>
        public static void StartInterval(double interval)
        {
            System.Timers.Timer myTimer = new System.Timers.Timer(interval);
            myTimer.Elapsed += new System.Timers.ElapsedEventHandler(StartInterval);
            myTimer.Interval = interval;
            myTimer.Enabled = true;
        }
        private static void StartInterval(object sender, System.Timers.ElapsedEventArgs e)
        {
            //要定时执行 的任务写在这儿
        }
        /// <summary>
        /// 间隔任务(单位:秒)
        /// </summary>
        /// <param name="hour">间隔秒,默认为1秒</param>
        public static void StartIntervalSecond(int second = 1)
        {
            double interval = second * 1000;
            System.Timers.Timer myTimer = new System.Timers.Timer(interval);
            myTimer.Elapsed += new System.Timers.ElapsedEventHandler(OnTimedEventSecond);
            myTimer.Interval = interval;
            myTimer.Enabled = true;
        }

        private static void OnTimedEventSecond(object sender, System.Timers.ElapsedEventArgs e)
        {
            //要定时执行 的任务写在这儿
        }


        /// <summary>
        /// 间隔任务(单位:分钟)
        /// </summary>
        /// <param name="hour">间隔分钟,默认为1分钟</param>
        public static void StartIntervalMinute(int minute = 1)
        {
            double interval = minute * 60 * 1000;
            System.Timers.Timer myTimer = new System.Timers.Timer(interval);
            myTimer.Elapsed += new System.Timers.ElapsedEventHandler(OnTimedEventMinute);
            myTimer.Interval = interval;
            myTimer.Enabled = true;
        }

        private static void OnTimedEventMinute(object sender, System.Timers.ElapsedEventArgs e)
        {
            //要定时执行 的任务写在这儿
        }
        /// <summary>
        /// 间隔任务(单位:小时)
        /// </summary>
        /// <param name="hour">间隔小时,默认为1小时</param>
        public static void StartIntervalHour(int hour = 1)
        {
            double interval = hour * 60 * 60 * 1000;
            System.Timers.Timer myTimer = new System.Timers.Timer(interval);
            myTimer.Elapsed += new System.Timers.ElapsedEventHandler(OnTimedEventHour);
            myTimer.Interval = interval;
            myTimer.Enabled = true;
        }

        private static void OnTimedEventHour(object sender, System.Timers.ElapsedEventArgs e)
        {
            //要定时执行 的任务写在这儿
            MeritPayTask.UpdateOnlineUser();
        }
        /// <summary>
        /// 间隔任务(单位:天)
        /// </summary>
        /// <param name="day">间隔天数,默认为1天</param>
        public static void StartIntervalDay(int day = 1)
        {
            double interval = day * 24 * 60 * 60 * 1000;
            System.Timers.Timer myTimer = new System.Timers.Timer(interval);
            myTimer.Elapsed += new System.Timers.ElapsedEventHandler(OnTimedEventDay);
            myTimer.Interval = interval;
            myTimer.Enabled = true;
        }

        private static void OnTimedEventDay(object sender, System.Timers.ElapsedEventArgs e)
        {
            //要定时执行 的任务写在这儿
            MeritPayTask.SetPayRollFeedBack();
        }
    }

    public enum TaskSchedulingType
    {
        Default = -1,
        Second = 0,
        Minute = 1,
        Hour = 2,
        Day = 3
    }
}


在Global.asax文件Application_Start中注册定时启动任务

using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Http;
using System.Web.Mvc;
using System.Web.Routing;
using System.Web.Security;
using System.Web.SessionState;
//using ZK.BLL;

namespace ZK.Web
{
    // 注意: 有关启用 IIS6 或 IIS7 经典模式的说明,
    // 请访问 http://go.microsoft.com/?LinkId=9394801
    public class MvcApplication : System.Web.HttpApplication
    {
        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();

            WebApiConfig.Register(GlobalConfiguration.Configuration);
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);

            log4net.Config.XmlConfigurator.ConfigureAndWatch(new FileInfo(Server.MapPath("Log4Net.config")));
            TaskScheduling.Initialize.Start(1, TaskScheduling.TaskSchedulingType.Day);  //按天执行
            TaskScheduling.Initialize.Start(1, TaskScheduling.TaskSchedulingType.Minute);  //按分钟执行
        }
    }
}




  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值