c#实时刷新Lable.text数据

用过timer只能设定时间间隔刷新数据,有没有不用timer刷新的,或者怎么利用多线程和while实现跨线程实时给label.text赋值

Thread+Invoke更新就行,最主要的是Invoke,要不会出错在其他线程更新控件内容,示例代码如下,有帮助麻烦点个采纳【本回答右上角】,谢谢~~有其他问题可以继续交流~

img

using System;
using System.Data;
using System.Windows.Forms;
using System.Threading;

namespace WindowsFormsApp1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            

        }
        private void button1_Click(object sender, EventArgs e)
        {
            new Thread(() => {
                while (true)
                {
                    this.Invoke(new Action(() =>
                    {
                        label1.Text = DateTime.Now.ToString();
                    }));
                    Thread.CurrentThread.Join(1000);//暂停1s
                }
            }).Start();
        }
    }
}


把Label控件当做参数传递到线程里面,直接在线程内部更新即可。