The code is as follows:
Public classTimetask {#regionSingle casePrivate StaticTimetask _task =NULL; Public Statictimetask Instance {Get { if(_task = =NULL) {_task=NewTimetask (); } return_task; } } #endregion //Events Public EventSystem.Timers.ElapsedEventHandler ExecuteTask; //Time Object PrivateSystem.Timers.Timer _timer =NULL; //defining the time interval Private int_interval = +;//default 1 seconds Public intInterval {Set{_interval=value; } Get { return_interval; } } //Start Public voidStart () {if(_timer = =NULL) {_timer=NewSystem.Timers.Timer (_interval); _timer. Elapsed+=NewSystem.Timers.ElapsedEventHandler (_timerelapsed); _timer. Enabled=true; _timer. Start (); } } //delegate method, mapped to the value passed in protected void_timerelapsed (Objectsender, System.Timers.ElapsedEventArgs e) { if(NULL!=executetask) {ExecuteTask (sender, E); } } //Stop Public voidStop () {if(_timer! =NULL) {_timer. Stop (); _timer. Dispose (); _timer=NULL; } } }
Calling method, in Global.asax, the code is as follows:
protected voidApplication_Start (Objectsender, EventArgs e) { //code to run when the application startsTimeTask.Instance.ExecuteTask + =NewSystem.Timers.ElapsedEventHandler (Timeexecutetask); TimeTask.Instance.Interval= +*Ten;//time interval, 10 secondsTimeTask.Instance.Start (); } voidTimeexecutetask (Objectsender, System.Timers.ElapsedEventArgs e) { //Write logic code here that needs to be executed at timed timeSystem.Diagnostics.Debug.WriteLine ("timed Task Execution"+DateTime.Now); }
Description: Because IIS recycles, you also need to configure the thread pool on IIS to not recycle it. As follows:
Recovery:
Fixed time interval (minutes) changed to 0
Virtual/private memory limit (KB) changed to 0
Process Model:
Idle timeout (minutes) changed to 0
ASP. NET Simple timed task implementation