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); //按分钟执行
}
}
}