asp.net a simple solution to perform a task at a fixed point in time _ practical skills

Source: Internet
Author: User
For example, send a report by email at seven o'clock in the morning every day.
The first idea here is to use the Global.asax file to implement the contents of the following Global file.
Copy Code code as follows:

<%@ application Language= "C #"%>
<%@ Import namespace= "System.Threading"%>

<script runat= "Server" >

This is used to keep a reference to this timer instance statically, lest the GC
private static System.Threading.Timer Timer = null;

protected void Application_Start (object sender, EventArgs e) {
Calculates the time period that is now to the target time.
DateTime lucktime = DateTime.Now.Date.Add (New TimeSpan (7, 0, 0));
TimeSpan span = Lucktime-datetime.now;
if (span < TimeSpan.Zero) {
span = lucktime.adddays (1d)-DateTime.Now;
}
The status or object to be passed on demand.
Object state = new Object ();
Defining timers
Timer = new System.Threading.Timer (
New TimerCallback (Certaintask), state,
span, Timespan.fromticks (timespan.ticksperday));
}

protected void Application_End (object sender, EventArgs e) {
At the end remember to release
if (timer!= null) {timer. Dispose (); }
}

This is the code that specifies the time to execute, and must be static.
private static void Certaintask (object state) {
Here, write your mission logic.
}

</script>

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.