在Global.asax启动一条线程就ok了,下面是启动线程定时写文件的例子
Global.asax
- <%@ 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 * 60);
- }
- }
- void Application_Start(object sender, EventArgs e)
- {
- LogPath = HttpContext.Current.Server.MapPath("log.txt");
-
- thread = new Thread(new ThreadStart(WriteLog));
- thread.Name = "写登录日志线程";
- thread.Start();
- }
-
- void Application_End(object sender, EventArgs e)
- {
-
-
- }
-
- void Application_Error(object sender, EventArgs e)
- {
-
-
- }
-
- void Session_Start(object sender, EventArgs e)
- {
-
-
- }
-
- void Session_End(object sender, EventArgs e)
- {
-
-
-
-
-
- }
-
- </script>
|