Timer 定时任务
程序员文章站
2024-01-23 14:58:40
...
public class OnlinePay
{
private static double dayEndStartTime = 0;
private static double dayEndStopTime = 0;
private static int retransmitInterval = 0;
private static System.Timers.Timer timer = null;//添加静态定时任务变量,
public OnlinePay()
{
if (dayEndStartTime == 0)
dayEndStartTime = Convert.ToDouble(ConfigurationManager.AppSettings["DayEndTimeRange_Start"].ToString());
if (dayEndStopTime == 0)
dayEndStopTime = Convert.ToDouble(ConfigurationManager.AppSettings["DayEndTimeRange_Stop"].ToString());
if (retransmitInterval == 0)
retransmitInterval = Convert.ToInt32(ConfigurationManager.AppSettings["RetransmitInterval"].ToString());
#region 定时任务
if (timer == null)
{
timer = new System.Timers.Timer(retransmitInterval * 1000);
timer.AutoReset = true;
timer.Enabled = true;
timer.Elapsed += Timer_Elapsed;
}
else
{
timer.Interval = retransmitInterval * 1000;
timer.Enabled = Convert.ToBoolean(ConfigurationManager.AppSettings["IsStartTask"].ToString());
}
#endregion
}
/// <summary>
/// 每小时重传一次OnlinePay数据
/// 间隔固定时间执行
/// </summary>
private void Timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
if (DateTime.Now.TimeOfDay > TimeSpan.FromHours(dayEndStartTime) || DateTime.Now.TimeOfDay < TimeSpan.FromHours(dayEndStopTime))
{
//不操作了,晚上歇歇
}
else
{
//给同步OnlinePay数据,每小时一次
}
}
}
Web.Config配置项
<!--DayEndTime Setting(start time: 22; stop time: 7),Dayend执行期间:晚上22点 < 当前时间 < 第二天早7点-->
<add key="DayEndTimeRange_Start" value="22"/>
<add key="DayEndTimeRange_Stop" value="7"/>
<!--Retransmit Interval(重发间隔:默认3600s)-->
<add key="RetransmitInterval" value="3600"/>
<!--是否启动定时任务,由于有负载均衡,所以添加一个开关(True/False)-->
<add key="IsStartTask" value="False"/>
<!--DayEndTime Setting(start time: 22; stop time: 7),Dayend执行期间:晚上22点 < 当前时间 < 第二天早7点-->
上一篇: Timer 定时任务
下一篇: 设备树 fixed-link 使用说明