最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時(shí)間:2014年01月28日 09:51:52   作者:  
這篇文章主要介紹了c#定時(shí)器和global實(shí)現(xiàn)自動(dòng)job示例,大家參考使用吧

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

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

相關(guān)文章

最新評(píng)論

枣庄市| 左云县| 庆云县| 天津市| 辽宁省| 聊城市| 信宜市| 中阳县| 西丰县| 广南县| 乡宁县| 汉源县| 平山县| 靖安县| 东辽县| 九台市| 格尔木市| 启东市| 哈尔滨市| 高要市| 马山县| 武城县| 兰溪市| 吉林市| 伊春市| 库尔勒市| 北票市| 安岳县| 瓦房店市| 贵德县| 达州市| 泸州市| 青冈县| 乌兰察布市| 尤溪县| 广河县| 余江县| 鄂州市| 山西省| 临江市| 海安县|