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