在Global.asax啟動(dòng)一條線(xiàn)程就ok了,下面是啟動(dòng)線(xiàn)程定時(shí)寫(xiě)文件的例子
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);//阻止1分鐘
- }
- }
- void Application_Start(object sender, EventArgs e)
- {
- LogPath = HttpContext.Current.Server.MapPath("log.txt");
- //在應用程序啟動(dòng)時(shí)運行的代碼
- thread = new Thread(new ThreadStart(WriteLog));
- thread.Name = "寫(xiě)登錄日志線(xiàn)程";
- thread.Start();
- }
- void Application_End(object sender, EventArgs e)
- {
- //在應用程序關(guān)閉時(shí)運行的代碼
- }
- void Application_Error(object sender, EventArgs e)
- {
- //在出現未處理的錯誤時(shí)運行的代碼
- }
- void Session_Start(object sender, EventArgs e)
- {
- //在新會(huì )話(huà)啟動(dòng)時(shí)運行的代碼
- }
- void Session_End(object sender, EventArgs e)
- {
- //在會(huì )話(huà)結束時(shí)運行的代碼。
- // 注意: 只有在 Web.config 文件中的 sessionstate 模式設置為
- // InProc 時(shí),才會(huì )引發(fā) Session_End 事件。如果會(huì )話(huà)模式
- //設置為 StateServer 或 SQLServer,則不會(huì )引發(fā)該事件。
- }
- </script>
聯(lián)系客服