The asp.net website executes a task at a specified time every day.

Source: Internet
Author: User

You can search online materials and test it on your own to meet your needs. If you find any problem, please point it out. Thank you.

The function of the case: automatically open the Baidu website around every day.

Main Code:

1. Global. asax File


[Cpp]
Void Application_Start (object sender, EventArgs e)
{
OpenBaiDu. Execute ();
}

Void Application_Start (object sender, EventArgs e)
{
OpenBaiDu. Execute ();
}

2. OpenBaiDu static class file


[Csharp]
Public static class OpenBaiDu
{
Public static bool IsOpen = false;
Public static DateTime LastOpenTime = DateTime. Now;
Public static DateTime OpenTime = DateTime. Today. AddHours (10 );
 
Public static void Execute ()
{
Timer objTimer = new Timer ();
ObjTimer. Interval = 1000; // 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 );
}
 
Public static void objTimer_Elapsed (object sender, ElapsedEventArgs e)
{
// If the last execution time is yesterday, set IsOpen to false, indicating that the task has not been executed today.
If (DateTime. Today. AddDays (-1) = LastOpenTime. Date)
{
IsOpen = false;
}
// If the task is not executed today and the current time is later than the specified execution time,
// After the execution is complete, set IsOpen to true, indicating that it has been executed today.
If (! IsOpen & DateTime. Now> = OpenTime)
{
System. Diagnostics. Process. Start ("http://www.baidu.com ");
IsOpen = true;
OpenTime = DateTime. Today;
}
 
}
}

Public static class OpenBaiDu
{
Public static bool IsOpen = false;
Public static DateTime LastOpenTime = DateTime. Now;
Public static DateTime OpenTime = DateTime. Today. AddHours (10 );

Public static void Execute ()
{
Timer objTimer = new Timer ();
ObjTimer. Interval = 1000; // 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 );
}

Public static void objTimer_Elapsed (object sender, ElapsedEventArgs e)
{
// If the last execution time is yesterday, set IsOpen to false, indicating that the task has not been executed today.
If (DateTime. Today. AddDays (-1) = LastOpenTime. Date)
{
IsOpen = false;
}
// If the task is not executed today and the current time is later than the specified execution time,
// After the execution is complete, set IsOpen to true, indicating that it has been executed today.
If (! IsOpen & DateTime. Now> = OpenTime)
{
System. Diagnostics. Process. Start ("http://www.baidu.com ");
IsOpen = true;
OpenTime = DateTime. Today;
}

}
}

 

Related Article

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.