Method one, using System;
Using System.Data;
Using System.Configuration;
Using System.Collections;
Using System.Web;
Using System.Web.Security;
Using System.Web.SessionState;
Using System.Timers;
Using System.Net;
Using System.IO;
Using System.Text;
Using System.Threading; Namespace Qumiao.com {public class Global:System.Web.HttpApplication {protected void Application_Start (obj
ECT sender, EventArgs e) {//define timer System.Timers.Timer MyTimer = new System.Timers.Timer (5000);
mytimer.elapsed + = new Elapsedeventhandler (mytimer_elapsed);
Mytimer.enabled = true;
Mytimer.autoreset = true; } void Mytimer_elapsed (object source, Elapsedeventargs e) {try {log.savenote (DateTime.Now.ToString ("yyyy-
MM-DD HH:mm:ss ") +": Autotask is working! ");
Yourtask ();
catch (Exception ee) {log.saveexception (EE); } void Yourtask () {//write the task you need to perform here} protected void Application_End (object sendEr, EventArgs e) {log.savenote (DateTime.Now.ToString ("Yyyy-mm-dd HH:mm:ss") + ": Application end!");
The following code is critical to solve the problem of automatic recycling of IIS application Pools thread.sleep (1000); Setting up your Web address, you can point to any of your ASPX pages or even nonexistent pages, in order to fire the Application_Start string url = http://www.shaoqun.com HttpWebRequest
myHttpWebRequest = (HttpWebRequest) webrequest.create (URL);
HttpWebResponse myhttpwebresponse = (HttpWebResponse) myhttpwebrequest.getresponse (); Stream Receivestream = Myhttpwebresponse.getresponsestream ();//Get Writeback Byte}}} Principle: Global.asax can be a asp.net application or Session event handler, we use the Application_Start (Application start Event) and Application_End (Application End event). When the application starts, start a timer to perform the task Yourtask () method, which can write the logic code that needs to be invoked, which can be single-threaded and multi-threaded. When an application is finished, such as the application pool recycle for IIS, let asp.net access the current web address. There is a need to access an ASPX page so that the application can be reactivated. The log class is a class for logging, and the following is the log information generated when testing: ================================================================ 2008-10-30
17:46:10:autotask is working!
2008-10-30 17:46:15:autotask is working!
2008-10-30 17:46:20:autotask is working!
2008-10-30 17:46:23:application end!
2008-10-30 17:46:29:autotask is working!
2008-10-30 17:46:34:autotask is working!
It is found from the log that the scheduled task is still executing after manually recycling the IIS application pool, indicating that our purpose has been achieved. If the code in Application_End is commented out, the scheduled task stops working after the application end is found, as follows: ===================================================
============= 2008-10-30 18:01:34:autotask is working!
2008-10-30 18:01:39:autotask is working!
2008-10-30 18:01:44:autotask is working!
2008-10-30 18:01:46:application end!
Limitations: The application pool can be resolved either automatically or manually, but it does not solve the problem of IIS rebooting or Web server restart, of course, when this happens, and if someone visits your site, it automatically activates the scheduled task. Scenario II, <%@ application language= "C #"%> <%@ import namespace= "System.IO"%> <script runat= "Server" &G
T void Application_Start (object sender, EventArgs e) {//code to run at application startup System.Timers.Timer MyTimer = new System.ti Mers.
Timer (10000);
mytimer.elapsed + = new System.Timers.ElapsedEventHandler (ontimedevent);
Mytimer.interval = 10000;
Mytimer.enabled = true; } void Application_end (object sender, EventArgs e) {//code that runs when the application shuts down} void Application_Error (object sender, EventArgs e) { Code that runs when an unhandled error occurs (object sender, EventArgs e) {//code that runs when a new session starts} void session Session_Start
_end (object sender, EventArgs e) {//code that runs at the end of the session. Note: The Session_End event is raised only if the sessionstate mode in the Web.config file is set to//InProc.
If the session mode is set to StateServer//or SQL Server, the event is not raised.
} private static void Ontimedevent (object source, System.Timers.ElapsedEventArgs e) {//interval time executing an action//specifying the directory of the log file
String Filelogpath = AppDomain.CurrentDomain.BaseDirectory + "Systemlog";
String filelogname = "Softprj_cn_" + DateTime.Now.ToLongDateString () + "_log.txt";
The definition file information object FileInfo finfo = new FileInfo (Filelogpath + filelogname); Creates a write-only file stream using (FileStream fs = Finfo.
Openwrite ()) {//create write data stream based on the file flow created above StreamWriter strwriter = new StreamWriter (FS); Sets the starting position of the write data stream to the end of the file stream strwriter. Basestream.seek (0, SeekOrigin.End); Write error occurs strwriter time.
WriteLine ("Occurrence time:" + DateTime.Now.ToString ()); Writes the contents of the log and wraps the line//strwriter.
WriteLine ("Error content:" + message); Strwriter.
WriteLine ("Error content:"); The Write interval character strwriter.
WriteLine ("---------------------------------------------"); Strwriter.
WriteLine (); Clears the contents of the buffer and writes the buffer contents to the underlying stream strwriter.
Flush (); Closes the write data stream strwriter.
Close (); Fs.
Close ();
} </script> Scenarios III, <%@ application language= "C #"%> <%@ Import namespace= "System.IO"%>
<%@ Import namespace= "system.threading"%> <script runat= "Server" > String LogPath;
Thread thread;
void Writelog () {while (true) {StreamWriter sw = new StreamWriter (LogPath, True, Encoding.UTF8); Sw. WriteLine (thread.
Name + ":" + DateTime.Now.ToString ()); Sw.
Close (); Thread.CurrentThread.Join (1000 * 10);//block 10 sec} void Application_Start (object sender, EventArgs e) {Logpa th = HttpContext.Current.Server.MapPath ("Log.txt"); Code thread that runs when the application starts = new Thread (new ThreadStart (Writelog)); Thread.
Name = write log log thread; Thread.
Start (); } void Application_End (object sender, EventArgs e) {//code that runs at application shutdown} void Application_Error (object send
Er, EventArgs e) {//code to run when an unhandled error occurs} void Session_Start (object sender, EventArgs e) {//code to run when a new session starts
} void Session_End (object sender, EventArgs e) {//code that runs at the end of the session. Note: The Session_End event is raised only if the sessionstate mode in the Web.config file is set to//InProc.
If the session mode is set to StateServer//or SQL Server, the event is not raised.
} </script>