• C# Winform使用线程,委托定时更新界面UI控件,解决界面卡顿问题(转载)


    一、定时执行主界面控件值

    1、利用定时器
            Thread t = null;
    
            private void InitTSJK()
            {
                t = new Thread(new ThreadStart(GetData));
                t.IsBackground = true;
                t.Start();
            }
    
            private void GetData()
            {
                while (true)
                {
                    QueryIro();
                    Thread.Sleep(10000);
                }
            }
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Data.SqlClient;
    using System.Drawing;
    using System.Linq;
    using System.Net;
    using System.Net.Sockets;
    using System.Text;
    using System.Threading;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    
    namespace SampleNotice
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
                Thread t = new Thread(new ThreadStart(GetData));
                t.IsBackground = true;
                t.Start();
            }
    
            private void GetData()
            {
                var timer = new System.Timers.Timer();
                timer.Interval = 1000;
                timer.Enabled = true;
                timer.AutoReset = true;//设置是执行一次(false)还是一直执行(true);  
                timer.Start();
                timer.Elapsed += (o, a) =>
                {
                    SetData();
                    ShowMessage(string.Format("更新时间:" + DateTime.Now));
                };
            }
    
            //声明委托
            private delegate void SetDataDelegate();
            private void SetData()
            {
                if (this.InvokeRequired)
                {
                    this.Invoke(new SetDataDelegate(SetData));
                }
                else
                {
                    label1.Text = string.Format("更新时间:" + DateTime.Now);
                }
            }
    
            //声明委托
            private delegate void ShowMessageDelegate(string message);
            private void ShowMessage(string message)
            {
                if (this.InvokeRequired)
                {
                    ShowMessageDelegate showMessageDelegate = ShowMessage;
                    this.Invoke(showMessageDelegate, new object[] { message });
                }
                else
                {
                    txtbox.Text = message;
                }
            }
        }
    }
  • 相关阅读:
    sqlserver调优-索引
    浮点数的这些坑,你未必知道-深入理解浮点数的规律
    生产环境部署springcloud微服务启动慢的问题排查
    redis传输协议规范-下(Redis Protocol specification)
    一步步完成“迷你版” 的ASP.NET Core框架
    安全漏洞整改系列(一)
    docker实战(二)之redis的使用
    docker实战(一)之Tomcat的安装
    docker安装步骤
    WPF后台操作前台元素之查找对象
  • 原文地址:https://www.cnblogs.com/michellexiaoqi/p/12206230.html
Copyright © 2020-2023  润新知