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: [www.bkjia.com] <% @ 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
}
</Script>