.net Timer定时执行

System.Timers.Timer可以实现数据库定时更新的功能

Global.asax

void Application_Start(object sender, EventArgs e) 
{
    // 在应用程序启动时运行的代码
    System.Timers.Timer timer = new System.Timers.Timer(1000);
    timer.Elapsed += new System.Timers.ElapsedEventHandler(AddCount);
    //AddCount是一个方法,此方法就是每个1秒而做的事情
    timer.AutoReset = true;
    //给Application["timer"]一个初始值
    Application.Lock();
    Application["timer"] = 1;
    Application.UnLock();
    timer.Enabled = true;

}

private void AddCount(object sender, System.Timers.ElapsedEventArgs e)
{
    Application.Lock();
    Application["timer"] = Convert.ToInt32(Application["timer"]) + 1;
    //这里可以写你需要执行的任务,比如说,清理数据库的无效数据或增加每个用户的积分等等
    Application.UnLock();
}

页面中调用Global中定义的值

string time = Application["timer"].ToString();
Label1.Text = time;
原文地址:https://www.cnblogs.com/LTEF/p/9243622.html