C# Winform使用线程,委托定时更新界面UI控件

本文介绍了如何在Winform应用中使用线程和委托来避免界面卡顿,通过定时器每秒更新UI控件,并在电场值变化时通过委托触发闪烁报警。
摘要由CSDN通过智能技术生成

 Winform使用线程,委托定时更新界面UI控件,解决界面卡顿问题

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;
            }
        }
    }
}

        #region 报警提示
        delegate void SetFlashElecCallback(int type);

        /// <summary>
        /// 检测到管涌位置电场值突然急剧增加需要有报警提示
        /// </summary>
        private void StartFlashElecThread(float elecValue)
        {
            if (float.TryParse(lblElectValue.Text, out float oldElecValue))
            {
                if (Math.Abs(elecValue-oldElecValue) >= DefaultConfig.SystemConfigs.ElecDiffValue)
                {
                    Thread elecThread = new Thread(SetFlashElecType);
                    elecThread.IsBackground = true;
                    elecThread.Start();
                }
            }
        }

        private void SetFlashElecType()
        {
            for (int i = 0; i<6; i++)
            {
                if (i % 2 == 0)
                    SetFlashElec(0);
                else
                    SetFlashElec(1);
                Thread.Sleep(1000);
            }
        }

        private void SetFlashElec(int type)
        {
            try
            {
                if (lblElectValue.InvokeRequired)
                {
                    while (!lblElectValue.IsHandleCreated)
                    {                        
                        if (lblElectValue.Disposing || lblElectValue.IsDisposed) 
                            return;
                    }
                    SetFlashElecCallback d = new SetFlashElecCallback(SetFlashElec);
                    lblElectValue.Invoke(d, new object[] { type });
                }
                else
                {
                    lblElectValue.ForeColor = type == 0 ? Color.Red : Color.FromArgb(175, 140, 255);
                }
            } 
            catch (Exception ex)
            {
                LogHelper.Instance.Write("FlashElecValue exception:"+ex.Message);
            }
        }
        #endregion

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值