c#定時(shí)器和global實(shí)現(xiàn)自動(dòng)job示例
一、創(chuàng)建一個(gè)cs文件,定義Time 對(duì)象
public class WebTimer_AutoRepayment
{
static WebTimer_AutoRepayment()
{
_WebTimerTask = new WebTimer_AutoRepayment();
}
/// <summary>
/// 實(shí)例化
/// </summary>
/// <returns></returns>
public static WebTimer_AutoRepayment Instance()
{
return _WebTimerTask;
}
/// <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í)器開始計(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>
/// 開始
/// </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)用所定義的方法
void Application_Start(object sender, EventArgs e)
{
//在應(yīng)用程序啟動(dòng)時(shí)運(yùn)行的代碼
WebTimer_AutoRepayment.Instance().Start(); //
}
void Application_End(object sender, EventArgs e)
{
//在應(yīng)用程序關(guān)閉時(shí)運(yùn)行的代碼
WebTimer_AutoRepayment.Instance().Stop();//
}
- c#定時(shí)器使用示例詳解
- C#定時(shí)器實(shí)現(xiàn)自動(dòng)執(zhí)行的方法
- c#一個(gè)定時(shí)重啟的小程序?qū)崿F(xiàn)代碼
- c#定時(shí)運(yùn)行程序分享(定時(shí)程序)
- ASP.NET(C#) 定時(shí)執(zhí)行一段代碼
- C#實(shí)現(xiàn)判斷一個(gè)時(shí)間點(diǎn)是否位于給定時(shí)間區(qū)間的方法
- C#多線程學(xué)習(xí)之(五)使用定時(shí)器進(jìn)行多線程的自動(dòng)管理
- C# 定時(shí)器定時(shí)更新的簡(jiǎn)單實(shí)例
- c# 在windows服務(wù)中 使用定時(shí)器實(shí)例代碼
- windows下C#定時(shí)管理器框架Task.MainForm詳解
相關(guān)文章
c#獲得目標(biāo)服務(wù)器中所有數(shù)據(jù)庫(kù)名、表名、列名的實(shí)現(xiàn)代碼
這篇文章主要介紹了c#獲得目標(biāo)服務(wù)器中所有數(shù)據(jù)庫(kù)名、表名、列名的方法,需要的朋友可以參考下2014-05-05C#使用AutoResetEvent實(shí)現(xiàn)同步
這篇文章主要為大家詳細(xì)介紹了C#使用AutoResetEvent實(shí)現(xiàn)同步的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-03-03c# 使用WebRequest實(shí)現(xiàn)多文件上傳
這篇文章主要介紹了c# 使用WebRequest實(shí)現(xiàn)多文件上傳的方法,幫助大家更好的理解和學(xué)習(xí)使用c#,感興趣的朋友可以了解下2021-03-03C#如何利用結(jié)構(gòu)體對(duì)固定格式數(shù)據(jù)進(jìn)行解析
這篇文章主要為大家詳細(xì)介紹了C#利用結(jié)構(gòu)體對(duì)固定格式數(shù)據(jù)進(jìn)行解析,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-01-01Unity實(shí)現(xiàn)老虎機(jī)滾動(dòng)抽獎(jiǎng)效果的示例代碼
這篇文章主要介紹了Unity實(shí)現(xiàn)老虎機(jī)滾動(dòng)抽獎(jiǎng)效果的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-04-04