当前位置: 首页 > 图文教程 > 网络编程 > ASP.NET > ASP.NET(C#) 定时执行一段代码

ASP.NET
安装IE补丁后ASP.NET将无法运行
在ASP.Net中应用Javascript
用ASP.NET 1.1 新特征防止Script攻击
ASP.NET中在线用户统计的简单实现及讨论
ASP.NET中将数据输出到Excel
在ASP.NET中从SQL Server检索图片
ASP.NET系统用户权限设计与实现
利用ASP.NET技术动态生成HTML页面
大数量查询分页显示 微软的解决办法
ASP.NET WEB页面多语言支持解决方案
ASP.NET 2.0里轻松获取数据库连接统计数据
ASP.NET通过DSO访问分析服务器的权限问题
ASP实现禁止从外部提交数据
Asp.Net 使用 GDI+ 绘制3D饼图入门篇源码
在ASP.NET中点击一个按钮后让它变灰的简单方法
利用JS在asp.net中实现左导航页的隐藏
asp.net中一次更新DATAGRID中所有记录
用Asp.net屏蔽F5、Ctrl+N、Alt+F4
asp.net中用C#实现站点计数器用户控件
认识ASP.NET配置文件Web.config

ASP.NET(C#) 定时执行一段代码


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2010-01-10   浏览: 80 ::
收藏到网摘: n/a

在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");
//在应用程序启动时运行的代码
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)
{
//在会话结束时运行的代码。
// 注意: 只有在 Web.config 文件中的 sessionstate 模式设置为
// InProc 时,才会引发 Session_End 事件。如果会话模式
//设置为 StateServer 或 SQLServer,则不会引发该事件。
}
</script>