zoukankan      html  css  js  c++  java
  • .net 定时服务

    namespace MvcApplication1 {    

        public class MvcApplication : System.Web.HttpApplication    

    {        

    protected void Application_Start()        

    {            

    AreaRegistration.RegisterAllAreas();

                WebApiConfig.Register(GlobalConfiguration.Configuration);  

               FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);      

           RouteConfig.RegisterRoutes(RouteTable.Routes);

                Time_Task.Instance().ExecuteTask += new System.Timers.ElapsedEventHandler(Global_ExecuteTask);  

               Time_Task.Instance().Interval = 1000 * 60;//表示间隔1分钟     

            Time_Task.Instance().Start();      

           //Time_Task.Instance().Stop();//结束定时

            }             

       public void Global_ExecuteTask(object sender, System.Timers.ElapsedEventArgs e)         {   

              //在这里编写需要定时执行的逻辑代码     

        }

        }

        public class Time_Task     {   

          public event System.Timers.ElapsedEventHandler ExecuteTask;

            private static readonly Time_Task _task = null;    

         private System.Timers.Timer _timer = null;     

        private int _interval = 1000;

            public int Interval         {   

              set             {                 _interval = value;             }         

        get             {                 return _interval;             }   

          }

            static Time_Task()         {    

             _task = new Time_Task();    

         }

            public static Time_Task Instance()         {   

              return _task;    

         }

            public void Start()         {   

              if (_timer == null)           

      {                

    _timer = new System.Timers.Timer(_interval);   

                  _timer.Elapsed += new System.Timers.ElapsedEventHandler(_timer_Elapsed);    

                 _timer.Enabled = true;        

             _timer.Start();     

            }    

         }

            protected void _timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)         {      

           if (null != ExecuteTask)         

        {                

    ExecuteTask(sender, e);     

            }    

         }

            public void Stop()     

        {           

      if (_timer != null)            

    {                

    _timer.Stop();          

           _timer.Dispose();   

                  _timer = null;  

               }        

    }    

    }

    }

  • 相关阅读:
    JSTL之迭代标签库
    java中的IO流
    浅谈代理模式
    TreeSet集合
    网络编程之UDP协议
    Java中的多线程
    Java中的面向对象
    JavaScript 函数表达式
    JavaScript 数据属性和访问器属性
    JavaScript 正则表达式语法
  • 原文地址:https://www.cnblogs.com/yinchuan/p/5477577.html
Copyright © 2011-2022 走看看