Web定时执行某个方法-网页获取

实现该功能用到的是System.Timers.Timer

 

 

将定时的方法添加到Global.ascx.cs中

public class Global : System.Web.HttpApplication

    {

        protected void Application_Start(object sender, EventArgs e)

        {



                //在应用程序启动时运行的代码

                System.Timers.Timer myTimer = new System.Timers.Timer(60000); // 每个一分钟判断一下,单位为毫秒

                myTimer.Elapsed += new System.Timers.ElapsedEventHandler(OnTimedEvent); //执行需要操作的代码,OnTimedEvent是要执行的方法名称

                myTimer.Interval = AutoUpdateGoldPriceTime;

                myTimer.Enabled = true;

            }

        }



        protected void Session_Start(object sender, EventArgs e)

        {

        }



        protected void Application_BeginRequest(object sender, EventArgs e)

        {



        }



        protected void Application_AuthenticateRequest(object sender, EventArgs e)

        {



        }



        protected void Application_Error(object sender, EventArgs e)

        {



        }



        protected void Session_End(object sender, EventArgs e)

        {



        }



        protected void Application_End(object sender, EventArgs e)

        {



        }



        protected  void OnTimedEvent(object source, System.Timers.ElapsedEventArgs e)

        {

        //需要定时执行的逻辑

        }        

 

你可能感兴趣的:(Web)