Execute scheduled tasks (multithreading) and scheduled tasks in Web Applications
In applications with complex services, sometimes one or more tasks are required to be scheduled at a certain time or interval, such as regular backup or database synchronization, scheduled sending of emails is called scheduled tasks. There are also many ways to schedule tasks. You can use SQLAgent to execute stored procedures, use Windows Task Scheduling programs, or use Windows Services to schedule tasks, these methods are good solutions. However, for Web applications, the implementation of these methods is not very simple, host service providers or cannot directly provide such services, or you need to pay a lot of extra fees. This article introduces a simple method that is directly used in Web applications. This method can be easily implemented without any additional configuration.
Because the ASP. NET Site runs as a Web application and is not restricted by threads, we can easily create and destroy a scheduler task in Application_Start and Application_End events. The following describes how to plan tasks on a Web site. In our example, information is regularly added to the file. As an example, the current time is regularly written to the file.
The unit of work of a scheduled task is called a Job. The following code describes a common interface that can be executed by the scheduling engine for all tasks, each task implements the Execute method for the scheduling engine to call:
Public interface ISchedulerJob
{
Void Execute ();
}
As mentioned above, our example is to write data to a file as a character date. The following describes how to implement this task:
Public class SampleJob: ISchedulerJob
{
Public void Execute ()
{
// The physical path of the file. CSTest is the virtual directory name, and F: InetpubwwwrootCSTest is the physical path.
String p = @ "F: InetpubwwwrootCSTest ";
// Create the SchedulerJob folder under the root directory of the virtual directory and set the permission to anonymous,
// SchedulerJob.txt is the file we wrote.
String FILE_NAME = p + "/SchedulerJob/SchedulerJob.txt ";
// Obtain the current server time and convert it to a string
String c = System. DateTime. Now. ToString ("yyyy-mm-dd hh: MM: ss ");
// Indicates whether the file is a scalar of the new file.
Bool flag = false;
// If the object does not exist, create the object.
If (! File. Exists (FILE_NAME ))
{
Flag = true;
StreamWriter sr = File. CreateText (FILE_NAME );
Sr. Close ();
}
// Write content to the file
StreamWriter x = new StreamWriter (FILE_NAME, true, System. Text. Encoding. Default );
If (flag) x. Write ("scheduled task test start :");
X. Write ("" + c );
X. Close ();
}
}
Next, we will create a configuration object to tell the scheduling engine what task to execute and the interval between the execution.
Public class SchedulerConfiguration
{
// Time Interval
Private int sleepInterval;
// Task list
Private ArrayList jobs = new ArrayList ();
Public int SleepInterval {get {return sleepInterval ;}}
Public ArrayList Jobs {get {return jobs ;}}
// Constructor of the scheduling configuration class
Public SchedulerConfiguration (int newSleepInterval)
{
SleepInterval = newSleepInterval;
}
}
The following is the scheduling engine, which periodically executes the task of the configuration object.
Public class Scheduler
{
Private SchedulerConfiguration configuration = null;
Public Scheduler (SchedulerConfiguration config)
{
Configuration = config;
}
Public void Start ()
{
While (true)
{
// Execute 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 describes how to activate the engine. In order to make our task plan run, we. asax. in the cs file Applicatio_Start and Application_End, create and destroy a thread where the scheduling process runs. The running interval here is 3 seconds.
Public System. Threading. Thread schedulerThread = null;
Protected void Application_Start (Object sender, EventArgs e)
{
SchedulerConfiguration config = new SchedulerConfiguration (1000*3 );
Config. Jobs. Add (new SampleJob ());
Scheduler scheduler = new Scheduler (config );
System. Threading. ThreadStart myThreadStart = new System. Threading. ThreadStart (scheduler. Start );
System. Threading. Thread schedulerThread = new System. Threading. Thread (myThreadStart );
SchedulerThread. Start ();
}
Finally, destroy the program upon exit:
Protected void Application_End (Object sender, EventArgs e)
{
If (null! = SchedulerThread)
{
SchedulerThread. Abort ();
}
}
All right, create a C # Web application project in VS. NET, create TaskScheduler. cs class, and modify the corresponding Global. asax. cs file. In order to see the effect, we create another form WebForm1.aspx and refresh it regularly to check the data we recorded:
<% @ Page language = "c #" Codebehind = "WebForm1.aspx. cs" AutoEventWireup = "false"
Inherits = "CSTest. WebForm1" %>
<! Doctype html public "-// W3C // dtd html 4.0 Transitional // EN">
<HTML>
<HEAD>
<Title> example of executing a scheduled task in a Web application </title>
<Meta http-equiv = "refresh" content = "10">
</HEAD>
<Body MS_POSITIONING = "GridLayout">
<Form id = "Form1" method = "post" runat = "server">
<Iframe style = "width: 100%; height: 100%" src = "SchedulerJob/SchedulerJob.txt"> </iframe>
</Form>
</Body>
</HTML>
Compile and run the project. The result is as follows:
Scheduled task test starts:
2003-13-10 11:08:15
2003-13-10 11:08:18
2003-13-10 11:08:21
2003-13-10 11:08:24
2003-13-10 11:08:27
11:08:30
It should be noted that the preceding is a simple example of executing a scheduled task in a Web application. For multiple tasks, they need to work in different threads, the plan is also very simple, and the actual site needs to be blocked, when the machine is in the case. In addition, there is no such work as error handling here. I believe everyone will write more perfect code.