您的位置:首页 > 编程语言 > C#

.net利用Timer和Global.asax实现定时执行程序C#

2010-06-13 15:17 826 查看
public class TestTimer
{

//TimerCallback 委托,GlobalTimer_ToDo表示要执行的方法
public 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)
{
if (DateTime.Now.Hour == 0)
{//判断多少时间执行一次

}
}

public static void Start(long a, long b)
{
//Timer.Change(Int32, Int32)方法用来更改计时器的启动时间和方法调用之间的间隔,用 32 位有符号整数度量时间间隔
GlobalTimer.Change(a, b);
}

public static void Stop()
{
//Timeout.Infinite是用于指定无限长等待时间的常数
GlobalTimer.Change(Timeout.Infinite, Timeout.Infinite);
}
//停止定时器调用TestTimer.Stop();释放定时器调用TestTimer.GlobalTimer.Dispose();
}
public class Global : System.Web.HttpApplication
{

protected void Application_Start(object sender, EventArgs e)
{
//参数0表示立即执行定时器,60000单位毫秒,就是一分钟执行一次 一小时 (3600000)
TestTimer.Start(0, 10000);
}

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)
{

}
}





天翼 三星W799 双模手机CDMA双摄FM双屏+JAVA炒股QQ地图2G内存
980.0元

宅男宅女,一起来约会吧!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: