zoukankan      html  css  js  c++  java
  • 定时器

      /// <summary>
        /// 定时器
        /// </summary>
        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()
            {
                VMCDUBusinessRequirement vMBusiness = new VMCDUBusinessRequirement();
                vMBusiness.UptBR();
            }
            #region Timer 计时器定义(0:20 / 6:20 启动)
            /// <summary>
            /// 调用 callback 的时间间隔。
            /// </summary>
            private static int Period = 6 * 60 * 60 * 1000;
            /// <summary>
            /// 调用 callback 之前延迟的时间量。
            /// </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 >= 50)
                    {
                        dueTime = 0;//立即启动
                    }
                    else
                    {
                        dueTime = (50 - minutes) * 60 * 1000;// 9:50 启动
                    }
                    WebTimerObj = new Timer(new TimerCallback(WebTimer_Callback), null, dueTime, Period);
                }
            }
            /// <summary>
            /// WebTimer的主函数
            /// </summary>
            /// <param name="sender"></param>
            private void WebTimer_Callback(object sender)
            {           
                try
                {
                    Log log = new Log(AppDomain.CurrentDomain.BaseDirectory + @"/log/TimerLog.txt");
                    if (Interlocked.Exchange(ref _IsRunning, 1) == 0)
                    {
                        ExecuteMain();
                        Times++;
                        Times = (Times % 100000);
                        log.log(" 启动定时器进行修改提醒状态;");
                    }
                }
                catch (Exception ex)
                {
                    Log log = new Log(AppDomain.CurrentDomain.BaseDirectory + @"/log/TimerMessageLog.txt");
                    log.log(" 错误提示:" + ex.ToString());
                }
                finally
                {
                    Interlocked.Exchange(ref _IsRunning, 0);
                }
            }
            /// <summary>
            /// 停止
            /// </summary>
            public void Stop()
            {
                if (WebTimerObj != null)
                {
                    WebTimerObj.Dispose();
                    WebTimerObj = null;
                }
            }
            #endregion
        }
    ///////////////////////////////////////////////////////////////////////////
    Global.asax
     
    protected void Application_Start()
            {
                //在应用程序启动时运行的代码
                WebTimer_AutoRepayment.Instance().Start();
                AreaRegistration.RegisterAllAreas();
                GlobalConfiguration.Configure(WebApiConfig.Register);
                FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
                RouteConfig.RegisterRoutes(RouteTable.Routes);
                BundleConfig.RegisterBundles(BundleTable.Bundles);
            }
            void Application_End(object sender, EventArgs e)
            {
                //在应用程序关闭时运行的代码
                WebTimer_AutoRepayment.Instance().Stop();
            }
  • 相关阅读:
    《Exceptional C++ Style中文版》 作者:Herb Sutter 定价39元
    11.24 《阿猫阿狗2》精美包装艳丽登场
    STLport 5.0.1 available for download.
    编程时需要注意的一些原则
    面向对象设计原则
    ASP.NET下MVC设计模式的实现
    string 与stringbuilder的区别
    工厂方法模式(Factory Method)
    面向对象设计(OOD)的基本原则
    HUTXXXX DNAANDDNA 贪心
  • 原文地址:https://www.cnblogs.com/nxj1997/p/13224778.html
Copyright © 2011-2022 走看看