国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 編程 > C# > 正文

c#定時(shí)器和global實(shí)現(xiàn)自動(dòng)job示例

2020-01-24 02:49:19
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

一、創(chuàng)建一個(gè)cs文件,定義Time 對(duì)象

復(fù)制代碼 代碼如下:

 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í)器開(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)用所定義的方法

復(fù)制代碼 代碼如下:

 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();//
    }

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 贡觉县| 井陉县| 通州市| 平顺县| 甘泉县| 清流县| 石狮市| 永川市| 高邑县| 新巴尔虎右旗| 天全县| 申扎县| 蒙山县| 名山县| 常山县| 馆陶县| 册亨县| 遂昌县| 行唐县| 云霄县| 渭源县| 鄢陵县| 同仁县| 宁强县| 横山县| 通海县| 自治县| 广西| 常宁市| 丰原市| 泸西县| 项城市| 霍林郭勒市| 安丘市| 林西县| 武鸣县| 楚雄市| 老河口市| 武清区| 崇明县| 鄂伦春自治旗|