C#线程的加入

progressBar的Step属性设置为1

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

namespace Demo1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            CheckForIllegalCrossThreadCalls = false;
        }
        Thread t1, t2;//全局变量
        void Pro1()
        {
            int count = 0;
            while (true)
            {
                progressBar1.PerformStep();//进度条按已设置的步长运行
                Thread.Sleep(100);
                count += progressBar1.Step;//统计进度                
                if (count==20)//到20%时,线程2加入
                {
                    t2.Join();
                }
            }
        }
        void Pro2()
        {
            int count = 0;
            while (true)
            {
                progressBar2.PerformStep();
                Thread.Sleep(100);
                count += progressBar1.Step;
                if (count==100)
                {
                    break;
                }
            }
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            t1 = new Thread(Pro1);
            t1.Start();
            t2 = new Thread(Pro2);
            t2.Start();
        }
    }
}

 

posted @ 2019-05-08 20:42  夕西行  阅读(202)  评论(0编辑  收藏  举报