ASP.NET(C#) 定時執(zhí)行一段代碼
更新時間:2009年11月26日 19:05:09 作者:
在Global.asax啟動一條線程就ok了,下面是啟動線程定時寫文件的例子.
Global.asax
C# code
<%@ 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");
//在應(yīng)用程序啟動時運行的代碼
thread = new Thread(new ThreadStart(WriteLog));
thread.Name = "寫登錄日志線程";
thread.Start();
}
void Application_End(object sender, EventArgs e)
{
//在應(yīng)用程序關(guān)閉時運行的代碼
}
void Application_Error(object sender, EventArgs e)
{
//在出現(xiàn)未處理的錯誤時運行的代碼
}
void Session_Start(object sender, EventArgs e)
{
//在新會話啟動時運行的代碼
}
void Session_End(object sender, EventArgs e)
{
//在會話結(jié)束時運行的代碼。
// 注意: 只有在 Web.config 文件中的 sessionstate 模式設(shè)置為
// InProc 時,才會引發(fā) Session_End 事件。如果會話模式
//設(shè)置為 StateServer 或 SQLServer,則不會引發(fā)該事件。
}
</script>
C# code
復(fù)制代碼 代碼如下:
<%@ 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");
//在應(yīng)用程序啟動時運行的代碼
thread = new Thread(new ThreadStart(WriteLog));
thread.Name = "寫登錄日志線程";
thread.Start();
}
void Application_End(object sender, EventArgs e)
{
//在應(yīng)用程序關(guān)閉時運行的代碼
}
void Application_Error(object sender, EventArgs e)
{
//在出現(xiàn)未處理的錯誤時運行的代碼
}
void Session_Start(object sender, EventArgs e)
{
//在新會話啟動時運行的代碼
}
void Session_End(object sender, EventArgs e)
{
//在會話結(jié)束時運行的代碼。
// 注意: 只有在 Web.config 文件中的 sessionstate 模式設(shè)置為
// InProc 時,才會引發(fā) Session_End 事件。如果會話模式
//設(shè)置為 StateServer 或 SQLServer,則不會引發(fā)該事件。
}
</script>
相關(guān)文章
Asp.net利用JQuery AJAX實現(xiàn)無刷新評論思路與代碼
Asp.net利用JQuery AJAX實現(xiàn)無刷新評論,此功能是每一個從事asp.net開發(fā)者的朋友都希望實現(xiàn)的,本文利用閑暇時間整理了一些,有需要的朋友可以參考下2012-12-12Asp.net TextBox的TextChanged事件使用介紹
動態(tài)創(chuàng)建的控件是如何加載視圖狀態(tài),還提到ProcessPostData方法的調(diào)用,這里我就用TextBox的TextChanged事件來說說視圖數(shù)據(jù)的加載以及事件的觸發(fā)2012-12-12Linux服務(wù)器下利用Docker部署.net Core項目的全過程
這篇文章主要給大家介紹了關(guān)于在Linux服務(wù)器下利用Docker部署.net Core項目的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家學(xué)習(xí)或者使用.net Core具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-07-07WPF實現(xiàn)ScrollViewer滾動到指定控件處
這篇文章主要為大家詳細介紹了WPF實現(xiàn)ScrollViewer滾動到指定控件處,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-06-06ASP.NET MVC5+EF6+EasyUI后臺管理系統(tǒng) 微信公眾平臺開發(fā)之消息管理
這篇文章主要介紹了ASP.NET MVC5+EF6+EasyUI后臺管理系統(tǒng),微信公眾平臺開發(fā)之消息管理,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-09-09ASP.NET中TextBox使用Ajax控件顯示日期不全的問題解決方法
這篇文章介紹了ASP.NET中TextBox使用Ajax控件顯示日期不全的問題解決方法,有需要的朋友可以參考一下2013-10-10.Net Core和jexus配置HTTPS服務(wù)方法
下面小編就為大家分享一篇.Net Core和jexus配置HTTPS服務(wù)方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-02-02