. NET Timer timed execution

Source: Internet
Author: User

System.Timers.Timer can realize the function of database timer update

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();}

The values defined in global are called in the page

string time = Application["timer"].ToString();Label1.Text = time;

. NET Timer timed execution

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.