System.Threading.Timer 定制Web服务器定时器执行事务!

本文介绍如何使用System.Threading.Timer在Web服务器上实现稳定的定时任务。通过将Timer定义为全局变量避免了IIS线程池垃圾回收导致的任务中断问题,并提供了一个使全局变量每秒递增的示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

System.Threading.Timer 定制Web服务器定时器执行事务!

 

原来在使用System.Threading.Timer的时候碰到一些问题,定时器在运行一段时间后,自动停止!原来是System.Threading.Timer运行一阵子后,IIS线程池开始垃圾回收的时候,timer被回收,
timer中的各项被Dispose。所以,定时器就实效了!

解决的方法是在Global中,定义System.Threading.Timer为全局变量,不是在方法中!在方法中定义就会被IIS线程池回收!

演示一个简单的例子:Application["A"]全局变量每一秒加1,只要IIS不停止网站,全局变量就会一直增加!

Global.aspx 代码:

public class Global : System.Web.HttpApplication
{
public static System.Threading.Timer stateTimer = null;

protected void Application_Start(object sender, EventArgs e)
{
Application["A"] = "0";
System.Threading.AutoResetEvent autoEvent = new System.Threading.AutoResetEvent(false);
System.Threading.TimerCallback timerDelegate = new System.Threading.TimerCallback(timerCall);
stateTimer = new System.Threading.Timer(timerDelegate, autoEvent, 0, 1000);
}

private void timerCall(object arts)
{
Application["A"] = (int.Parse(Application["A"].ToString()) + 1).ToString();
}
}

MSDN资料:

语法

C#
public Timer(
TimerCallback callback,
Object state,
int dueTime,
int period
)


参数
callback
类型:System.Threading.TimerCallback

一个 TimerCallback 委托,表示要执行的方法。

state
类型:System.Object

一个包含回调方法要使用的信息的对象,或者为 nullNothingnullptrnull 引用(在 Visual Basic 中为 Nothing)。

dueTime
类型:System.Int32

调用 callback 之前延迟的时间量(以毫秒为单位)。指定 Timeout.Infinite 可防止启动计时器。指定零 (0) 可立即启动计时器。

period
类型:System.Int32

调用 callback 的时间间隔(以毫秒为单位)。指定 Timeout.Infinite 可以禁用定期终止。

### 使用 System.Threading.Timer 每隔 5 秒执行一次操作 在 C# 中,`System.Threading.Timer` 是一种基于线程池的定时器,可以用来每隔一定时间间隔执行特定的任务。以下是实现每隔 5 秒执行一次操作的具体方法: #### 关键点说明 - `System.Threading.Timer` 需要四个参数: 1. **TimerCallback** 委托,定义了定时器触发时要执行的操作。 2. **state 对象**,传递给回调方法的数据(通常为 `null` 表示不传递数据)。 3. **dueTime**,首次启动前的延迟时间(以毫秒为单位)。设置为 `0` 可立即启动。 4. **period**,后续每次触发的时间间隔(以毫秒为单位)。设置为 `Timeout.Infinite` 则只触发一次。 对于本需求,需将 `period` 设置为 5000 毫秒(即 5 秒)[^2]。 #### 示例代码 以下是一个完整的示例程序,展示如何使用 `System.Threading.Timer` 每隔 5 秒执行一次任务: ```csharp using System; using System.Threading; class Program { static void Main() { Console.WriteLine($"System.Threading.Timer started at {DateTime.Now}"); // 创建一个新的 Timer 实例 var timer = new Timer( callback: Progress, state: null, dueTime: 0, // 立即启动第一次执行 period: 5000 // 每隔 5 秒执行一次 ); // 防止主线程退出 Console.ReadLine(); // 如果需要手动停止计时器,在适当位置调用 Dispose 方法 // timer.Dispose(); } private static void Progress(object state) { Console.WriteLine($"Progress method executed at {DateTime.Now}"); } } ``` #### 解析 上述代码中: - `callback: Progress` 定义了一个名为 `Progress` 的静态方法作为回调函数,当定时器到期时会自动调用该方法并打印当前时间戳。 - 参数 `state` 被设为 `null`,因为此处无需向回调方法传递额外的状态信息。 - `dueTime` 设定为 `0`,意味着创建计时器后立刻执行第一次任务。 - `period` 设定为 `5000`,代表之后每隔 5 秒重复执行一次任务。 需要注意的是,尽管 `System.Threading.Timer` 性能较高且适合短期任务调度,但如果长时间运行可能会因垃圾回收机制而终止工作[^4]。因此,在长期运行场景下应考虑其他替代方案,比如显式管理生命周期或改用独立线程。 --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值