Since the ASP.NET site is run as a web application, it is not limited by the thread, so we can easily create and destroy a planned task in Application_Start and Application_end events. Let's take a brief introduction to the method of implementing planning tasks in the Web site. Our example is to add information to the file timing, as an example, where the current time is written to the file. The work unit of a planned task is called a task (JOB). The following code describes a universal interface that can be performed by the scheduled engine plan for all tasks. Each task here implements the Execute method for the transfer engine. Call: public interface ischedulerJob {void Execute ();} As mentioned, our example is to implement the document, such as the character date, the following is a method of implementing this task: public class samplejob: ischadulerjob {public void execute () { // The physical path saved by the file, cstest is the virtual directory name, f: / inetpub / wwwroot / cstest is the physical path string p = @ "f: / inetpub / wwwroot / cstest"; // We are in the root of the virtual directory Establish the SCHEDULERJOB folder, and set the permissions to modify, // schedulerJob.txt is the file string file_name = p "//schedulerjob/schedulerJob.txt" ;// to get the current server time, and convert into characters String string c = system.datetime.now.tostring ("YYYY-MM-DD HH: MM: SS"); / / Tag is the sputum flag of the new file BOOL flag = false; // If the file does not exist, File if (! File.exists (file_name)) {flag = true; streamwriter sr = file.createtext (file_name); sr.close ();} // Write content streamwriter x = new streamwriter (file_name, true, System.Text.Encoding.default); if (flag) x.write ("Scheduled task test start:"); x.write ("/ r / n" c); x.close ();}} Next We create a configuration object that tells the scheduled engine to perform what tasks and intervals of execution.
public class SchedulerConfiguration {// interval private int sleepInterval; // task list private ArrayList jobs = new ArrayList (); public int SleepInterval {get {return sleepInterval;}} public ArrayList Jobs {get {return jobs;}} // Scheduling constructors public arranged class SchedulerConfiguration (int newSleepInterval) {sleepInterval = newSleepInterval;}} the following is the scheduling engine, the timing of execution configuration object tasks public class scheduler {private SchedulerConfiguration configuration = null; public scheduler (SchedulerConfiguration config) {configuration = config; } public void Start () {while (true) {// perform each task foreach (ISchedulerJob job in configuration.Jobs) {ThreadStart myThreadDelegate = new ThreadStart (job.Execute); Thread myThread = new Thread (myThreadDelegate); myThread. Start (); thread.sleep (configuration.sleepinterval);}}}} All preparations have been completed, the following is the work of the activation engine. In order to make our task plan, we create and destroy jobs in Applicatio_Start and Application_end in the global.asax.cs file, first create a thread running running, we have a running interval here for 3 seconds. // define a thread variable public System.Threading.Thread schedulerThread = null; protected void Application_Start (Object sender, EventArgs e) {// example scheduling configuration SchedulerConfiguration config = new SchedulerConfiguration (1000 * 3); // add task config.Jobs .Add (new SampleJob ()); Scheduler scheduler = new Scheduler (config); // create ThreadStart delegate System.Threading.ThreadStart myThreadStart = new System.Threading.ThreadStart (scheduler.Start); // instantiate thread System.Threading .Thread schedulerThread = new System.Threading.Thread (myThreadStart); // start the thread schedulerThread.Start ();} Finally, you need to be destroyed when the program exits: protected void Application_End (Object sender, EventArgs e) {if (null! = Schedulerthread.abort ();}} Well, establish a C # web application engineering in VS.NET, establish the taskscheduler.cs class, and modify the corresponding global.asax.cs file.