.net全局定时定期执行某些操作在Global.asax中具体实现


复制代码 代码如下:

<%@ Application Language="C#" %>
<%@ import Namespace="System.Data" %>
<%@ import Namespace="System.Data.SqlClient" %>
<%@ import Namespace="System.Collections" %>
<%@ Import Namespace="System.IO" %>
//引入类库
<script runat="server">
//add by chairuirui 2013-3-26
void Application_Start(object sender, EventArgs e)
{
//在应用程序启动时运行的代码
System.Timers.Timer myTimer = new System.Timers.Timer(60000); // 每个一分钟判断一下
myTimer.Elapsed += new System.Timers.ElapsedEventHandler(OnTimedEvent); //执行需要操作的代码,OnTimedEvent是要执行的方法名称
myTimer.Interval = 60000;
myTimer.Enabled = true;
}
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,则不会引发该事件。
}
private static void OnTimedEvent(object source, System.Timers.ElapsedEventArgs e)
{
//需要的操作写在这个方法中
}

« 
» 
快速导航

Copyright © 2016 phpStudy | 豫ICP备2021030365号-3