.net定时执行任务(可实现自动更新)

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方法。

posted @ 2010-09-17 17:09  rob_2010  阅读(245)  评论(0)    收藏  举报