您的位置:首页 > 其它

Web定时执行某个方法-网页获取

2013-08-24 08:45 225 查看
实现该功能用到的是System.Timers.Timer

将定时的方法添加到Global.ascx.cs中

public class Global : System.Web.HttpApplication
{
protected 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 = AutoUpdateGoldPriceTime;
myTimer.Enabled = true;
}
}

protected void Session_Start(object sender, EventArgs e)
{
}

protected void Application_BeginRequest(object sender, EventArgs e)
{

}

protected void Application_AuthenticateRequest(object sender, EventArgs e)
{

}

protected void Application_Error(object sender, EventArgs e)
{

}

protected void Session_End(object sender, EventArgs e)
{

}

protected void Application_End(object sender, EventArgs e)
{

}

protected  void OnTimedEvent(object source, System.Timers.ElapsedEventArgs e)
{
//需要定时执行的逻辑
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: