There was a problem with IIS releasing the timer object when using the System.Threading.Timer object in one of the previous projects. Honestly, I've never had this problem before. That is, I previously defined the timer object is not released, running normally, I came back after the Baidu search for this information, the original IIS in the run WebApp for non-static resources are automatically released, and I looked back at the previous written Web program, Luckily it was written in this way:
Global.asax file
private static Timer time; System.Threading;
private static log log;
protected void Application_Start (object sender, EventArgs e)
{
log = new log ();
Log. Write (ref time, 5000);
}
Log.cs Code:
Class log{public
void Write (ref Timer Time,int flashtime)
{
if (time = = null) {Time
= new Timer (New Ti Mercallback (doexecution), this, 0,
flashtime);
}
void Doexecution (Object obj)
{
//Timed execution Code
}
}
That is to say, the timer object is defined as a global static object will not be released by IIS, if not so write, will certainly be depressed for a while when the error. But now a wide range of knowledge, scheduled to perform tasks can use quartz.net open source components, he encapsulated the time object, you can make the task more stable execution, the following show sample code:
public class TimeJob:Quartz.IJob {public
void execute (quartz.jobexecutioncontext context)
{
//code to be executed regularly ...
}
}
public class Global:System.Web.HttpApplication
{
private static Timer time;
protected void Application_Start (object sender, EventArgs e)
{
//define task
quartz.jobdetail job = new Quartz.jobdetail ("Job1", "group1", typeof
(Timejob));
Define trigger
Quartz.trigger Trigger = Quartz.TriggerUtils.MakeSecondlyTrigger (5);//5 seconds to
execute
Trigger. Name = "Trigger1";
Trigger. Jobgroup = "Group1";
Trigger. JobName = "Job1";
Trigger. Group = "Group1";
The definition plans
quartz.ischedulerfactory SF = new Quartz.Impl.StdSchedulerFactory ();
Quartz.ischeduler sch = sf. Getscheduler ();
Sch. AddJob (Job, true);//Add Task
Sch. Schedulejob (trigger);//Add Planner
Sch. Start ();//Begin Execution
}
}
The above code is also defined in the Global.asax file.
Back to the column page: http://www.bianceng.cnhttp://www.bianceng.cn/webkf/aspx/