ASP.NET(C#) 定時(shí)執(zhí)行一段代碼
更新時(shí)間:2009年11月26日 19:05:09 作者:
在Global.asax啟動(dòng)一條線程就ok了,下面是啟動(dòng)線程定時(shí)寫文件的例子.
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)用程序啟動(dòng)時(shí)運(yùn)行的代碼
thread = new Thread(new ThreadStart(WriteLog));
thread.Name = "寫登錄日志線程";
thread.Start();
}
void Application_End(object sender, EventArgs e)
{
//在應(yīng)用程序關(guān)閉時(shí)運(yùn)行的代碼
}
void Application_Error(object sender, EventArgs e)
{
//在出現(xiàn)未處理的錯(cuò)誤時(shí)運(yùn)行的代碼
}
void Session_Start(object sender, EventArgs e)
{
//在新會(huì)話啟動(dòng)時(shí)運(yùn)行的代碼
}
void Session_End(object sender, EventArgs e)
{
//在會(huì)話結(jié)束時(shí)運(yùn)行的代碼。
// 注意: 只有在 Web.config 文件中的 sessionstate 模式設(shè)置為
// InProc 時(shí),才會(huì)引發(fā) Session_End 事件。如果會(huì)話模式
//設(shè)置為 StateServer 或 SQLServer,則不會(huì)引發(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)用程序啟動(dòng)時(shí)運(yùn)行的代碼
thread = new Thread(new ThreadStart(WriteLog));
thread.Name = "寫登錄日志線程";
thread.Start();
}
void Application_End(object sender, EventArgs e)
{
//在應(yīng)用程序關(guān)閉時(shí)運(yùn)行的代碼
}
void Application_Error(object sender, EventArgs e)
{
//在出現(xiàn)未處理的錯(cuò)誤時(shí)運(yùn)行的代碼
}
void Session_Start(object sender, EventArgs e)
{
//在新會(huì)話啟動(dòng)時(shí)運(yùn)行的代碼
}
void Session_End(object sender, EventArgs e)
{
//在會(huì)話結(jié)束時(shí)運(yùn)行的代碼。
// 注意: 只有在 Web.config 文件中的 sessionstate 模式設(shè)置為
// InProc 時(shí),才會(huì)引發(fā) Session_End 事件。如果會(huì)話模式
//設(shè)置為 StateServer 或 SQLServer,則不會(huì)引發(fā)該事件。
}
</script>
您可能感興趣的文章:
- c#定時(shí)器使用示例詳解
- C#定時(shí)器實(shí)現(xiàn)自動(dòng)執(zhí)行的方法
- c#一個(gè)定時(shí)重啟的小程序?qū)崿F(xiàn)代碼
- c#定時(shí)運(yùn)行程序分享(定時(shí)程序)
- C#實(shí)現(xiàn)判斷一個(gè)時(shí)間點(diǎn)是否位于給定時(shí)間區(qū)間的方法
- C#多線程學(xué)習(xí)之(五)使用定時(shí)器進(jìn)行多線程的自動(dòng)管理
- c#定時(shí)器和global實(shí)現(xiàn)自動(dòng)job示例
- C# 定時(shí)器定時(shí)更新的簡單實(shí)例
- c# 在windows服務(wù)中 使用定時(shí)器實(shí)例代碼
- windows下C#定時(shí)管理器框架Task.MainForm詳解
相關(guān)文章
ASP.NET MVC5驗(yàn)證系列之Fluent Validation
這篇文章主要為大家詳細(xì)介紹了ASP.NET MVC5驗(yàn)證系列之Fluent Validation,感興趣的小伙伴們可以參考一下2016-07-07
aspx文件格式使用URLRewriter實(shí)現(xiàn)靜態(tài)化變成html
如何隱藏aspx文件格式,變成html,使用asp.net 開發(fā)的網(wǎng)頁程序,使用URLRewriter.dll 實(shí)現(xiàn)靜態(tài)化,接下來將介紹下具體操作步驟,感興趣的朋友可以參考下2013-04-04
Visual Studio IDE編寫程序時(shí)不顯示窗口或窗口一閃而逝的解決方法
這篇文章主要為大家詳細(xì)介紹了Visual Studio IDE編寫程序時(shí)不顯示窗口或窗口一閃而逝的解決方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-09-09
ASP.NET Repeater 單雙行數(shù)據(jù)換色示例
ASP.NET Repeater 單雙行數(shù)據(jù)換色根據(jù)點(diǎn)擊Repeater的ItemCreated觸發(fā)事件來時(shí)實(shí)現(xiàn)換色2014-10-10
Asp.net mvc 權(quán)限過濾和單點(diǎn)登錄(禁止重復(fù)登錄)
這篇文章主要介紹了Asp.net mvc 權(quán)限過濾和單點(diǎn)登錄(禁止重復(fù)登錄)的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-12-12
為TextBox裝飾水印與(blur和focus)事件應(yīng)用
為了界面的美觀,有些時(shí)候可能需要為文本框(TextBox)裝飾個(gè)水?。凰袃煞N狀態(tài),一是blur和focus。因此,我們可以在Javascript寫兩個(gè)事件,感興趣的朋友可以了解下2013-01-01
asp.net簡單實(shí)現(xiàn)單點(diǎn)登錄(SSO)的方法
這篇文章主要介紹了asp.net簡單實(shí)現(xiàn)單點(diǎn)登錄(SSO)的方法,結(jié)合簡單實(shí)例形式較為詳細(xì)的分析了單點(diǎn)登錄的原理與asp.net的相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2015-12-12

