C# 使用System.Timers.Timer更新控件(使用Invoke)

System.Timers.Timer可以定时执行方法,即在指定的时间间隔之后执行事件。本文讨论的是利用System.Timers.Timer来定时更新控件。

form窗体上放Label,用来指示利用System.Timers.Timer更新控件是否成功,成功则更改文本来提示。

如果在System.Timers.Timer的Elapsed绑定的方法中直接直接更新Label的文本,则会报异常“线程间操作无效: 从不是创建控件“txtLog”的线程访问它。”

using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Test
{
    public partial class Form_select_station : Form
    {
        private System.Timers.Timer timer = null;
        public Form_select_station(String tb_name)
        {
            InitializeComponent();
            if (timer == null)
            {
                timer = new System.Timers.Timer();
                timer.Interval = 201;
                timer.AutoReset = true;
                timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed);   
            }
        }

        private void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            CreateControlsByTimer();
        }

        private void CreateControlsByTimer()
        {
            //创建一个委托,用于封装一个方法,在这里是封装了 控制更新控件 的方法
            Action invokeAction = new Action(CreateControlsByTimer);
            //判断操作控件的线程是否创建控件的线程
            //调用方调用方位于创建控件所在的线程以外的线程中,如果在其他线程则对控件进行方法调用时必须调用 Invoke 方法
            if (this.InvokeRequired)
            {
                //与调用线程不同的线程上创建(说明您必须通过 Invoke 方法对控件进行调用)
                this.Invoke(invokeAction);
            }
            else
            {
                //窗体线程,即主线程
				lbl.Text = "Timer已跨线程更新了控件Label";
                Console.WriteLine("");
            }
        }

    }
}

而上面的代码里面用到了Invoke方法,这个方法用于“在拥有控件的基础窗口句柄的线程上,用指定的参数列表执行指定委托”。

就是说Label控件是在主线程上创建的,而System.Timers.Timer实际上是另一个线程,所以在它的线程即Elapsed绑定的事件上,使用Invoke方法委托主线程更改Label的文本(这里先判断如果是在System.Timers.Timer的线程上,则执行Invoke(Action)。之后回到主线程后,再更新Label控件的文本)。

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值