当前位置: 移动技术网 > IT编程>开发语言>c# > c#定时器和global实现自动job示例

c#定时器和global实现自动job示例

2019年07月18日  | 移动技术网IT编程  | 我要评论
一、创建一个cs文件,定义time 对象复制代码 代码如下: public class webtimer_autorepayment{  &n

一、创建一个cs文件,定义time 对象

复制代码 代码如下:

 public class webtimer_autorepayment
{
    static webtimer_autorepayment()
    {
        _webtimertask = new webtimer_autorepayment();
    }
    /// <summary>
    /// 实例化
    /// </summary>
    /// <returns></returns>
    public static webtimer_autorepayment instance()
    {
        return _webtimertask;
    }

    /// <summary>
    /// 实际执行的方法
    /// </summary>
    private void executemain()
    {
        //定义你自己要执行的job
        chinapnrinterfaces.autosendrepaymentnotice();//定时发送短信提醒的方法
    }
    #region timer 计时器定义
    /// <summary>
    /// 调用 callback 的时间间隔(以毫秒为单位)。指定 timeout.infinite 可以禁用定期终止。
    /// </summary>
    private static int period = 1 * 60 * 60 * 1000;
    /// <summary>
    /// 调用 callback 之前延迟的时间量(以毫秒为单位)。指定 timeout.infinite 以防止计时器开始计时。指定零 (0) 以立即启动计时器。
    /// </summary>
    private static int duetime = 3 * 1000;//三分钟后启动
    /// <summary>
    ///第几次执行
    /// </summary>
    private long times = 0;
    /// <summary>
    /// 实例化一个对象
    /// </summary>
    private static readonly webtimer_autorepayment _webtimertask = null;
    private timer webtimerobj = null;
    /// <summary>
    /// 是否正在执行中
    /// </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;//立即启动
            }
            else
            {
                duetime = (55 - minutes) * 60 * 1000;//到某个时间点的55分钟启动
            }
            webtimerobj = new timer(new timercallback(webtimer_callback), null, duetime, period);
        }
    }
    /// <summary>
    /// webtimer的主函数
    /// </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文件中调用所定义的方法

复制代码 代码如下:

 void application_start(object sender, eventargs e)
    {
        //在应用程序启动时运行的代码
        webtimer_autorepayment.instance().start(); //
    }

    void application_end(object sender, eventargs e)
    {
        //在应用程序关闭时运行的代码
        webtimer_autorepayment.instance().stop();//
    }

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网