ASP.NET定时执行某任务
2010-09-30 09:50
295 查看
服务器端采用C#语法:
1.在Global.asax文件中导入命名空间
<%@ Import Namespace="System.Timers" %>
2.Global.asax文件中的Application_Start()方法内写如下代码:
System.Timers.Timer objTimer = new Timer();
objTimer.Interval = 时间; //这个时间单位毫秒,比如10秒,就写10000
objTimer.Enabled = true;
objTimer.Elapsed += new ElapsedEventHandler(objTimer_Elapsed);
3.Global.asax文件中添加一个方法
void objTimer_Elapsed(object sender, ElapsedEventArgs e)
{
//这个方法内实现你想做的事情。
//例如:修改Application的某一个值等等。
}
以上3步则可以在指定时间间隔执行这个objTimer_Elapsed()方法,即达到你要得效果
===================================================
using System;
using System.Timers;
namespace 定时器ConsoleApplication1
{
class Class1
{
[STAThread]
static void Main(string[] args)
{
System.Timers.Timer aTimer = new System.Timers.Timer();
aTimer.Elapsed += new ElapsedEventHandler(TimeEvent);
// 设置引发时间的时间间隔 此处设置为1秒(1000毫秒)
aTimer.Interval = 1000;
aTimer.Enabled = true;
Console.WriteLine("按回车键结束程序");
Console.WriteLine(" 等待程序的执行......");
Console.ReadLine();
}
// 当时间发生的时候需要进行的逻辑处理等
// 在这里仅仅是一种方式,可以实现这样的方式很多.
private static void TimeEvent(object source, ElapsedEventArgs e)
{
// 得到 hour minute second 如果等于某个值就开始执行某个程序。
int intHour = e.SignalTime.Hour;
int intMinute = e.SignalTime.Minute;
int intSecond = e.SignalTime.Second;
// 定制时间; 比如 在10:30 :00 的时候执行某个函数
int iHour = 10;
int iMinute = 30;
int iSecond = 00;
// 设置 每秒钟的开始执行一次
if( intSecond == iSecond )
{
Console.WriteLine("每秒钟的开始执行一次!");
}
// 设置 每个小时的30分钟开始执行
if( intMinute == iMinute && intSecond == iSecond )
{
Console.WriteLine("每个小时的30分钟开始执行一次!");
}
// 设置 每天的10:30:00开始执行程序
if( intHour == iHour && intMinute == iMinute && intSecond == iSecond )
{
Console.WriteLine("在每天10点30分开始执行!");
}
}
}
}
1.在Global.asax文件中导入命名空间
<%@ Import Namespace="System.Timers" %>
2.Global.asax文件中的Application_Start()方法内写如下代码:
System.Timers.Timer objTimer = new Timer();
objTimer.Interval = 时间; //这个时间单位毫秒,比如10秒,就写10000
objTimer.Enabled = true;
objTimer.Elapsed += new ElapsedEventHandler(objTimer_Elapsed);
3.Global.asax文件中添加一个方法
void objTimer_Elapsed(object sender, ElapsedEventArgs e)
{
//这个方法内实现你想做的事情。
//例如:修改Application的某一个值等等。
}
以上3步则可以在指定时间间隔执行这个objTimer_Elapsed()方法,即达到你要得效果
===================================================
using System;
using System.Timers;
namespace 定时器ConsoleApplication1
{
class Class1
{
[STAThread]
static void Main(string[] args)
{
System.Timers.Timer aTimer = new System.Timers.Timer();
aTimer.Elapsed += new ElapsedEventHandler(TimeEvent);
// 设置引发时间的时间间隔 此处设置为1秒(1000毫秒)
aTimer.Interval = 1000;
aTimer.Enabled = true;
Console.WriteLine("按回车键结束程序");
Console.WriteLine(" 等待程序的执行......");
Console.ReadLine();
}
// 当时间发生的时候需要进行的逻辑处理等
// 在这里仅仅是一种方式,可以实现这样的方式很多.
private static void TimeEvent(object source, ElapsedEventArgs e)
{
// 得到 hour minute second 如果等于某个值就开始执行某个程序。
int intHour = e.SignalTime.Hour;
int intMinute = e.SignalTime.Minute;
int intSecond = e.SignalTime.Second;
// 定制时间; 比如 在10:30 :00 的时候执行某个函数
int iHour = 10;
int iMinute = 30;
int iSecond = 00;
// 设置 每秒钟的开始执行一次
if( intSecond == iSecond )
{
Console.WriteLine("每秒钟的开始执行一次!");
}
// 设置 每个小时的30分钟开始执行
if( intMinute == iMinute && intSecond == iSecond )
{
Console.WriteLine("每个小时的30分钟开始执行一次!");
}
// 设置 每天的10:30:00开始执行程序
if( intHour == iHour && intMinute == iMinute && intSecond == iSecond )
{
Console.WriteLine("在每天10点30分开始执行!");
}
}
}
}
相关文章推荐
- 利用Cache,asp.net 简单实现定时执行任务
- ASP.NET定时执行任务方法分享
- ASP.NET 定时执行任务 global.asax
- ASP.NET 定时执行任务 global.asax
- 推荐一个简单、轻量、功能非常强大的C#/ASP.NET定时任务执行管理器组件–FluentScheduler定时器
- ASP.NET 定时执行任务(定时器)
- 【ASP.NET】定时执行任务
- ASP.NET定时执行任务
- ASP.NET定时执行任务 (不使用外接程序,.net内部机制实现)
- asp.net web 服务器端全局定时执行任务
- ASP.NET定时执行某任务
- 推荐一个简单、轻量、功能非常强大的C#/ASP.NET定时任务执行管理器组件–FluentScheduler
- C#/ASP.NET定时任务执行管理器组件–FluentScheduler定时器
- asp.net web 定时执行任务 定时器 Global.asax
- 基于ASP.NET MVC(C#)和Quartz.Net组件实现的定时执行任务调度
- ASP.NET定时执行任务
- asp.net如何定时执行任务
- ASP.NET 定时执行任务(定时器)
- ASP.Net中定时执行任务的方法
- 使用ASP.NET实现Windows Service定时执行任务