timer传参

C# 线程中使用Timer,并向其触发的事件(函数)中传递参数 收藏
C#中的三个Timer我就不作说明了,这里要说的是如果你在线程中使用Timer,是不能使用System.Windows.Forms.Timer的,微软的注释很明确:“实现按用户定义的时间间隔引发事件的计时器。此计时器最宜用于 Windows 窗体应用程序中,并且必须在窗口中使用。”,在线程中使用,其相应的事件是不会触发的。

在什么地方使用什么样的Timer不是本文所要谈论的。这里仅对在使用Timer的时候的参数传递进行探讨。

■①在System.Windows.Forms.Timer 中我们如果需要在Timer所触发的事件中传递一个参数可以用一下属性Tag,也就是在启动Timer的时候给这个属性赋值,在被触发之后是可以从中取出并使用的:

如:Timer触发的事件为:void _ringTimer_Tick(object source, EventArgs e),则可将source转型为Timer,然后就可以得到启动时传递给属性Tag的参数值。

■②假若有这样一个需求:在一个线程中需要不定时启动多个Timer,每个Timer所触发的事件是相同的,但需要根据某一个参数在该事件中执行不同的操作(反正就是需要一个参数传递到Timer所触发的事件中),但System.Timers.Timer 所有属性中并不包含可用于传递参数的属性,因而要想在启动Timer的时候注入一个参数,以便在Timer触发的函数中使用,就不那么方便了。

System.Windows.Forms.Timer中具有Tag属性,System.Timers.Timer 如果也有一个Tag或更多的最好是自定义的属性岂不更好,是的,我们可以根据需要自定一个Timer即可:

view plaincopy to clipboardprint?
/// <summary> 
/// 带有自定义参数的System.Timers.Timer 
/// </summary> 
public class MyTimer : System.Timers.Timer 

    public object Tag1 { get; set; } 
    public object Tag2 { get; set; } 
    public object Tag3 { get; set; } 
    public object Tag4 { get; set; } 

 

贴一个调试图片:

 

下面是全部代码:

view plaincopy to clipboardprint?
using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Text; 
using System.Windows.Forms; 
using System.Threading; 
using System.Timers; 
namespace WindowsFormsApplication10 

    public partial class Form1 : Form 
    { 
        public Form1() 
        { 
            InitializeComponent(); 
        } 
        private Thread testThread; 
        private void Form1_Load(object sender, EventArgs e) 
        { 
            testThread = new Thread(ThreadStartMethod); 
            testThread.Start(); 
        } 
        /// <summary> 
        /// 线程执行的函数 
        /// </summary> 
        public void ThreadStartMethod() 
        { 
            while (true) 
            { 
                //启动一个Timer 
                MyTimer _timer = WaitRingTimer; 
                _timer.Tag1 = DateTime.Now; 
                _timer.Tag2 = "参数2"; 
                _timer.Tag3 = "参数3"; 
                _timer.Tag4 = "参数4"; 
                _timer.Start(); 
                Thread.Sleep(50000);//休息一下 
            } 
        } 
 
        /// <summary> 
        /// 带有自定义参数的System.Timers.Timer 
        /// </summary> 
        public class MyTimer : System.Timers.Timer 
        { 
            public object Tag1 { get; set; } 
            public object Tag2 { get; set; } 
            public object Tag3 { get; set; } 
            public object Tag4 { get; set; } 
        } 
        public MyTimer WaitRingTimer 
        { 
            get 
            { 
                MyTimer t = new MyTimer() { Interval = 2000 };//实例化Timer类,设置间隔时间为2000毫秒;   
                t.Elapsed += new ElapsedEventHandler(timer1_Tick);//Timer触发的事件timer1_Tick; 
                t.AutoReset = true;//设置是执行一次(false)还是一直执行(true);    
                return t; 
            } 
        } 
        /// <summary> 
        /// Timer所触发的函数 
        /// </summary> 
        /// <param name="sender">事件源</param> 
        /// <param name="e">包含事件数据的 System.Timers.ElapsedEventArgs 对象</param> 
        private void timer1_Tick(object sender, ElapsedEventArgs e) 
        { 
            MyTimer _timer = (MyTimer)sender; 
            DateTime tag1 = (DateTime)_timer.Tag1; 
            string tag2 = _timer.Tag2.ToString(); 
            string tag3 = _timer.Tag3.ToString(); 
            string tag4 = _timer.Tag4.ToString(); 
        } 
    } 

转载于:https://www.cnblogs.com/biewenjun/archive/2012/04/06/2435190.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值