ASP. NET scheduled task execution (timer)

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.