ASP. NET scheduled task execution (timer) [Date:] Source: Net explorer Author: Unknown [Font: large medium small]
The server adopts the C # Syntax:
1. Import the namespace in the global. asax File
<% @ Import namespace = "system. Timers" %>
2. Write the application_start () method in the global. asax file as follows:Code:
System. Timers. Timer objtimer = new timer ();
Objtimer. interval = time; // The unit of time is millisecond. For example, if the value is 10 seconds, the value is 10000.
Objtimer. Enabled = true;
Objtimer. elapsed + = new elapsedeventhandler (objtimer_elapsed );
3. Add a method to the Global. asax file.
Void objtimer_elapsed (Object sender, elapsedeventargs E)
{
// Implement what you want to do in this method.
// For example, modify a value of the application.
}
You can specify
Using system;
Using system. Timers;
Namespace timer leleapplication1
{
Class class1
{
[Stathread]
Static void main (string [] ARGs)
{
System. Timers. Timer atimer = new system. Timers. Timer ();
Atimer. elapsed + = new elapsedeventhandler (timeevent );
// Set the interval of the triggering time to 1 second (1000 ms)
Atimers. interval = 1000;
Atimer. Enabled = true;
Console. writeline ("press enter to end Program ");
Console. writeline ("waiting for program execution ......");
Console. Readline ();
}
// Logic processing required when time occurs
// Here is only one method, which can be implemented in many ways.
Private Static void timeevent (Object source, elapsedeventargs E)
{
// Obtain the hour minute second. If it is equal to a value, a program is executed.
Int inthour = E. signaltime. hour;
Int intminute = E. signaltime. minute;
Int intsecond = E. signaltime. Second;
// Customize the time. For example, execute a function.
Int ihour = 10;
Int iminute = 30;
Int isecond = 00;
// Set the execution to start once every second
If (intsecond = isecond)
{
Console. writeline ("the execution starts once every second! ");
}
// Set the execution to start 30 minutes each hour
If (intminute = iminute & intsecond = isecond)
{
Console. writeline ("the execution starts once every 30 minutes! ");
}
// Set the program to start running at 10:30:00 every day
If (inthour = ihour & intminute = iminute & intsecond = isecond)
{
Console. writeline ("Run at every day! ");
}
}
}
}
Execute the objtimer_elapsed () method at intervals to achieve the desired effect.