.net利用Global.asax创建定时执行程序

using System;
using System.Threading;

static public class TestTimer
{

   
static Timer GlobalTimer = new Timer(new TimerCallback(GlobalTimer_ToDo), null, Timeout.Infinite, Timeout.Infinite);

/*也可以直接定时

 GlobalTimer.Interval = 10;
        GlobalTimer.Enabled = true;
        GlobalTimerAutoReset = true;*/
   
static void GlobalTimer_ToDo(object obj)
    {
       
//todo : 这里做该做的事
    }

   
static public void Start(long a, long b)
    {
        GlobalTimer.Change(a, b);
    }

   
static public void Stop()
    {
        GlobalTimer.Change(Timeout.Infinite, Timeout.Infinite);
    }

}

这样,就可以在Global.asax中启动它,例如: 
HTML code
<%@ Application Language="C#" %> <script RunAt="server"> void Application_Start(object sender, EventArgs e) { TestTimer.Start(0, 2000); }



你可以在任何类中访问这个TestTimer类,直接调用它的Start、Stop方法。

原文地址:https://www.cnblogs.com/dfsxh/p/1459157.html