线程操作UI的几种方法

这篇博客探讨了在C#环境下如何使用多线程来实现UI元素的异步更新,包括三种不同的方法:标志位+执行位、上升沿和简单方法。通过示例代码展示了如何在后台线程中安全地修改UI组件的状态,并确保线程安全。同时,博客还涉及到了线程同步和委托的使用。
摘要由CSDN通过智能技术生成

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

namespace Thread_Test
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();


        }

        Thread Thread_1;
        Thread Thread_2;
        Thread Thread_3;
        bool App_Open = false;
        private void Form1_Load(object sender, EventArgs e)
        {
            App_Open = true;
            //
            Thread_1 = new Thread(Running1);
            Thread_1.Start();

            Thread_2 = new Thread(Running2);
            Thread_2.Start();

            Thread_3 = new Thread(Running3);
            Thread_3.Start();
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            App_Open = false;
            Thread.Sleep(100);
        }

        #region 方法1**标志位+执行位
        bool Point_Do = false;
        bool Point_Set = true;

        private void Btn_Hand_Click(object sender, EventArgs e)
        {
            if (Point_Set == true)
            {
                Point_Set = false;
            }
            else
            {
                Point_Set = true;
            }

            Point_Do = true;
        }

        private void Running1()
        {

            while (App_Open == true)
            {
                Thread.Sleep(10);

                try
                {
                    if (Point_Do == true)
                    {
                        Point_Do = false;
                        //线程中要求改主窗体UI中的text属性
                        this.SetText(Point_Set);
                    }
                }
                catch (Exception ex)
                {
                    string Info_Err = ex.ToString();
                    //---日志记录
                }
            }
        }

        //创建代理
        delegate void SetTextCallback(bool Bool_1);
        //调用窗体中的函数用invoke传递参数
        private void SetText(bool Bool_1)
        {
            if (this.Btn_Result1.InvokeRequired)
            {
                SetTextCallback d = new SetTextCallback(SetText);
                this.Invoke(d, new object[] { Bool_1 });
            }
            else
            {
                if (Bool_1 == true)
                {
                    Btn_Result1.BackColor = Color.Green;
                    Btn_Result1.Text = "OK";
                }
                else
                {
                    Btn_Result1.BackColor = Color.Red;
                    Btn_Result1.Text = "NG";
                }
            }
        }
        #endregion

        #region 方法2**上升沿
        bool Point_Do2 = true;
        private void Btn_Hand2_Click(object sender, EventArgs e)
        {
            if (Point_Do2 == true)
            {
                Point_Do2 = false;
            }
            else
            {
                Point_Do2 = true;
            }
        }

        private void Running2()
        {
            bool Point_X = false;

            while (App_Open == true)
            {
                try
                {
                    Thread.Sleep(10);

                    if (Point_X != Point_Do2)
                    {
                        Point_X = Point_Do2;

                        //线程中要求改主窗体UI中的text属性
                        this.SetText2(Point_X);
                    }
                }
                catch (Exception ex)
                {
                    string Info_Err = ex.ToString();
                    //---日志记录
                }
            }
        }

        //创建代理
        delegate void SetTextCallback2(bool Bool_1);
        //调用窗体中的函数用invoke传递参数
        private void SetText2(bool Bool_1)
        {
            if (this.Btn_Result1.InvokeRequired)
            {
                SetTextCallback2 d = new SetTextCallback2(SetText2);
                this.Invoke(d, new object[] { Bool_1 });
            }
            else
            {
                if (Bool_1 == true)
                {
                    Btn_Result2.BackColor = Color.Green;
                    Btn_Result2.Text = "OK";
                }
                else
                {
                    Btn_Result2.BackColor = Color.Red;
                    Btn_Result2.Text = "NG";
                }
            }
        }
        #endregion

        #region 方法3**简单
        bool Point_Do3 = true;
        private void Btn_Hand3_Click(object sender, EventArgs e)
        {
            if (Point_Do3 == true)
            {
                Point_Do3 = false;
            }
            else
            {
                Point_Do3 = true;
            }
        }

        private void Running3()
        {
            while (App_Open == true)
            {
                Thread.Sleep(100);
                try
                {
                    Invoke(new Action(() =>
                    {
                        if (Point_Do3 == true)
                        {
                            Btn_Result3.Text = "OK";
                            Btn_Result3.BackColor = Color.Green;
                        }
                        else
                        {
                            Btn_Result3.Text = "NG";
                            Btn_Result3.BackColor = Color.Red;
                        }
                    }));
                }
                catch (Exception ex)
                {
                    string Info_Err = ex.ToString();
                    //---日志记录
                }
            }
        }
        #endregion

    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值