在System.timers.timer中给定时器触发的Elapsed事件传递参数


一个项目中用到大量的定时器,用来做定时的任务。比如定时给某个客户发送短信之类的。现在定时器触发事件之后需要给事件传递一个参数过去。其中可能包括这个任务的一些必需的信息,System.Timers.Timer的事件处理方法如下:

public abstract void TimerEvent(object sender, ElapsedEventArgs e);

先看了看 timer,和ElapsedEventArgs 中有没有能利用起来的属性。发现不可能。

于是 打算继承 ElapsedEventArgs ,用来传递参数。但是找不到设置timer发送继承之后的ElapsedEventArgs 的方法,这种办法也不行。

最后继承System.Timers.Timer

using System;

namespace TaskSchedulerManager
{
///
/// 自定义定时器
///
public class TaskTimer : System.Timers.Timer
{
#region <变量>
///
/// 定时器id
///
private int id;
///
/// 定时器参数
///

private string param ;
#endregion

#region <属性>
///
/// 定时器id属性
///
public int ID
{
set{id = value;}
get{return id;}
}
///
/// 定时器参数属性
///
public string Param
{
set{param = value;}
get{return param;}
}
#endregion

///
/// 构造函数
///
public TaskTimer() : base ()
{

}

}
}

然后在事件处理方法中:将sender 转型为System.timers.timer

///
/// timer Elapsed事件的处理方法
///
///


///
public override void TimerEvent(object sender, System.Timers.ElapsedEventArgs e)
{
//Console.WriteLine("DllTwo:" + DateTime.Now.ToString());

TaskSchedulerManager.TaskTimer tt = (TaskTimer)sender;
Console.WriteLine("id : " + tt.ID);
Console.WriteLine("Param: " + tt.Param);
}

  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值