In the Web program to implement the timer, there is a certain difficulty, the browser is actually only need to use the JS timer can be achieved, but how to implement the server? are implemented using Global.asax plus timer, but many articles do not mention this design problem.
The basic code is simple:
Global.asax File Contents:
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;//10 minutes timer = new System.Timers.Timer (Interval);//10 minutes 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 to run at application Startup 6//Castle.ActiveRecord.Framework.Config.XmlConfigurationSource Source = new Castle.ActiveRecord.Fr Amework. Config.xmlconfigurationsource (".. /.. /appconfig.xml "); Iconfigurationsource Source = System.Configuration.ConfigurationManager.GetSection ("ActiveRecord") as Iconfigurationsource; Castle.ActiveRecord.ActiveRecordStarter.Initialize (System.Reflection.Assembly.Load ("eas.entity"), source);// You can target any class D under the assemblyHc. EAS. Common.LogInfo.Info ("The current application initialization is complete."); } public void Send (object sender, System.Timers.ElapsedEventArgs e) {DHC. EAS. Bo.SendMsg.Send (); } protected void Application_BeginRequest (Object sender, EventArgs e) {} void Application_End (object sender, EventArgs e) {//The code that runs when the application shuts down DHC. EAS. Common.LogInfo.Info ("The current application is closed"); New DHC. EAS. Common.appexception ("The current application is closed"); if (timer! = NULL) {timer. Stop (); Timer. Close (); timer = null; }} void Application_Error (object sender, EventArgs e) {//code that runs when an unhandled error occurs Exception EX = Server.GetLastError (). GetBaseException (); New DHC. EAS. Common.appexception ("The current application has errors", ex); Dispose of the abnormal server.clearerror () in time; } void Session_Start (object sender, EventArgs e) {//code//DHC that runs when a new session is started. EAS. Common.LogInfo.Info ("Reply time 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 at the end of the session. Note: The Session_End event is raised only if the sessionstate mode in the Web. config file is set to//InProc. If the session mode is set to StateServer//or SQL Server, the event is not raised. DHC.EAS.Common.LogInfo.Info ("End a Session =" + Session.SessionID); }
In fact, there are two points to consider 1 Application_Start must occur, 2 IIS may be recycled, causing the Application_End event to occur
1application_start must happen
application_start will not happen, the timer will not start, timing will not be able to talk about.
2application_end event
For example, publish a file , modify the Webconfig and so will lead to events, Of course there is no access for a long time, and IIS's recycling mechanism can also cause it. Then the timer will not exist, application_end After the incident,
In fact, this timing operation, I personally recommend using the service program to deal with.
Httpruntime.cache Timing Reference
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
ASP. Web Timer Implementation and considerations