一、創(chuàng)建一個(gè)cs文件,定義Time 對(duì)象
/// <summary>
/// 實(shí)際執(zhí)行的方法
/// </summary>
private void ExecuteMain()
{
//定義你自己要執(zhí)行的Job
ChinaPnrInterfaces.AutoSendRepaymentNotice();//定時(shí)發(fā)送短信提醒的方法
}
#region Timer 計(jì)時(shí)器定義
/// <summary>
/// 調(diào)用 callback 的時(shí)間間隔(以毫秒為單位)。指定 Timeout.Infinite 可以禁用定期終止。
/// </summary>
private static int Period = 1 * 60 * 60 * 1000;
/// <summary>
/// 調(diào)用 callback 之前延遲的時(shí)間量(以毫秒為單位)。指定 Timeout.Infinite 以防止計(jì)時(shí)器開(kāi)始計(jì)時(shí)。指定零 (0) 以立即啟動(dòng)計(jì)時(shí)器。
/// </summary>
private static int dueTime = 3 * 1000;//三分鐘后啟動(dòng)
/// <summary>
///第幾次執(zhí)行
/// </summary>
private long Times = 0;
/// <summary>
/// 實(shí)例化一個(gè)對(duì)象
/// </summary>
private static readonly WebTimer_AutoRepayment _WebTimerTask = null;
private Timer WebTimerObj = null;
/// <summary>
/// 是否正在執(zhí)行中
/// </summary>
private int _IsRunning;
/// <summary>
/// 開(kāi)始
/// </summary>
public void Start()
{
if (WebTimerObj == null)
{
DateTime now = DateTime.Now;
int minutes = now.Minute;
if (minutes >= 55)
{
dueTime = 0;//立即啟動(dòng)
}
else
{
dueTime = (55 - minutes) * 60 * 1000;//到某個(gè)時(shí)間點(diǎn)的55分鐘啟動(dòng)
}
WebTimerObj = new Timer(new TimerCallback(WebTimer_Callback), null, dueTime, Period);
}
}
/// <summary>
/// WebTimer的主函數(shù)
/// </summary>
/// <param name="sender"></param>
private void WebTimer_Callback(object sender)
{
try
{
if (Interlocked.Exchange(ref _IsRunning, 1) == 0)
{
ExecuteMain();
Times++;
Times = (Times % 100000);
}
}
catch
{
}
finally
{
Interlocked.Exchange(ref _IsRunning, 0);
}
}
/// <summary>
/// 停止
/// </summary>
public void Stop()
{
if (WebTimerObj != null)
{
WebTimerObj.Dispose();
WebTimerObj = null;
}
}
#endregion
}
二、在Global文件中調(diào)用所定義的方法
新聞熱點(diǎn)
疑難解答
圖片精選