simple solution for scheduled task execution

Source: Internet
Author: User

A task is executed at a specific time point every day.

For example, a report is sent by email at every day.

The first thing that comes to mind here is to use the Global. asax file to implement the content of the following Global file.

Copy to ClipboardReference: [] <% @ Application Language = "C #" %>
<% @ Import Namespace = "System. Threading" %>

<Script runat = "server">

// Here, use static hold to reference the Timer instance to avoid GC
Private static System. Threading. Timer timer = null;

Protected void Application_Start (object sender, EventArgs e ){
// Calculate the time period from the current time 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 that is passed as needed.
Object state = new object ();
// Define the timer
Timer = new System. Threading. Timer (
New TimerCallback (CertainTask), state,
Span, TimeSpan. FromTicks (TimeSpan. TicksPerDay ));

Protected void Application_End (object sender, EventArgs e ){
// Remember to release at the end
If (timer! = Null) {timer. Dispose ();}

// The Code executed at the specified time must be static.
Private static void CertainTask (object state ){
// Write your task logic here


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: 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.