tao5644
tao5644
2017-10-16 05:29

c#多线程处理控件问题

  • c#
  • 线程
  • 多线程

1.要实现label1 和label12 显示自加,两个线程同时执行。
2.目前显示刷新存在卡顿
3.怎么做到互不干扰,异步执行,类似单个线程,快速刷新界面?







using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        int a2 = 0, b2 = 0,c2=0;
        Thread a, b;

        public Form1()
        {
            InitializeComponent();
            a = new Thread(a4);
            b = new Thread(b4);

        }
        private void button2_Click(object sender, EventArgs e)
        {
            a.Start();

        }
        private void button3_Click(object sender, EventArgs e)
        {
            b.Start();
        }
        private void a4()
        {

            while (true)
            {
                a1();
            }
        }

        private void b4()
        {

            while (true)
            {
                b1();
            }
        }



        private delegate void ThreadDelegate();
        private void a1()
        {
            if (label1.InvokeRequired)
            {
                ThreadDelegate a3 = new ThreadDelegate(a1);
                label1.Invoke(a3);
            }
            else
            {

                    a2 = a2 + 1;
                    label1.Text = Convert.ToString(a2);

            }
        }


        private void b1()
        {
            if (label2.InvokeRequired)
            {
                ThreadDelegate b3 = new ThreadDelegate(b1);
                label2.Invoke(b3);

            }
            else
            {

                    b2 = b2 + 1;
                    label2.Text = Convert.ToString(b2);

            }

        }





    }
}
  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

2条回答