Implementation and precautions of asp.net web Timer

Source: Internet
Author: User

Implementation and precautions of asp.net web Timer

 

It is difficult to implement a timer on a web program. In fact, you only need to use a js timer on the browser side. But how does the server implement the timer? All are implemented using Global. asax and Timer, but this design issue is not mentioned in many articles.

 

The basic code is simple:

Contents of the Global. asax file:

 

System. Timers. Timer timer = null; void Application_Start (object sender, EventArgs e) {if (timer! = Null) {timer. stop (); timer. close (); timer = null;} int Interval = 1000*60*10; // ten minute timer = new System. timers. timer (Interval); // ten minute timer. elapsed + = new System. timers. elapsedEventHandler (Send); timer. interval = Interval; timer. enabled = true; timer. start (); DHC. EAS. common. logInfo. info (the current application is being initialized ......); // Code 6 that runs when the application starts // Castle. activeRecord. framework. config. xmlConfigurationSource source = New Castle. activeRecord. framework. config. xmlConfigurationSource (.. /.. /appconfig. xml); IConfigurationSource source = System. configuration. configurationManager. getSection (activerecord) as IConfigurationSource; Castle. activeRecord. activeRecordStarter. initialize (System. reflection. assembly. load (Eas. entity), source); // applicable to any class DHC. EAS. common. logInfo. info (the current application Initialization is complete .);} public void Send (object se Nder, System. timers. elapsedEventArgs e) {DHC. EAS. bo. sendMsg. send ();} protected void Application_BeginRequest (Object sender, EventArgs e) {} void Application_End (object sender, EventArgs e) {// code DHC that runs when the application is closed. EAS. common. logInfo. info (the current application is disabled); new DHC. EAS. common. appException (the current application is disabled); if (timer! = Null) {timer. stop (); timer. close (); timer = null ;}} void Application_Error (object sender, EventArgs e) {// code Exception that runs when an unprocessed error occurs ex = Server. getLastError (). getBaseException (); new DHC. EAS. common. appException (current application error, ex); // clear the exception Server in time after processing. clearError ();} void Session_Start (object sender, EventArgs e) {// code that runs when a new session is started // DHC. EAS. common. logInfo. info (Session interval (minutes) + Session. timeout. toString (); // DHC. EAS. common. logInfo. info (start a Session = + Session. sessionID);} void Session_End (object sender, EventArgs e) {// code that runs when the session ends. // Note: The Session_End event is triggered only when the sessionstate mode in the Web. config file is set to // InProc. If the session mode is set to StateServer // or SQLServer, this event is not triggered. // DHC. EAS. Common. LogInfo. Info (end a Session = + Session. SessionID );}

In fact, there are two points to consider that 1 Application_Start must occur, and 2 IIS may be recycled, resulting in an Application_End event.

 

1Application_Start must occur

Users are required to access the webpage. If no one accesses the webpage, Application_Start will not occur, timer will not start, and timing will not be available.

2Application_End event

For example, if a file is published and webconfig is modified, the Application_End event will occur. Of course, there will be no access for a long time, as will the IIS recycle mechanism. Then there will be no timer, and there will be no way to talk about timing. In fact, after this, you can access your website through code to ensure that Application_Start occurs after the Application_End event occurs.

In fact, someone suggested using HttpRuntime. Cache to add a variable, set an expiration time, and call the required operations through the callback function during expiration. Can this method be used? I did not test it. It is theoretically feasible, but I personally feel that there will still be problems above.

In fact, for this scheduled operation, I personally suggest using a service program for processing.

HttpRuntime. Cache timing reference

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.