日期:2014-05-17  浏览次数:20478 次

多线程progressBar 问题 在线等 急~~~~~~
想要实现 进度条赛跑 的功能 用一个随机产生正数 函数控制线程的快慢 该怎么做啊?

------解决方案--------------------
C# code

随即数你就自己写吧。代码丑了点。实现思路丑了点。不过能达到效果

Thread th1, th2, th3, th4;
        int AddNum1, AddNum2, AddNum3, AddNum4;

        private void button1_Click(object sender, EventArgs e)
        {
            int Num1 = 1;
            int Num2 = 2;
            int Num3 = 3;
            int Num4 = 4;
            AddNum1 = Num1;
            AddNum2 = Num2; AddNum3 = Num3; 
            AddNum4 = Num4;
            th1 = new Thread(new ThreadStart(delegate { ThMeod(Num1); }));
            th1.Start();
            th2 = new Thread(new ThreadStart(delegate { ThMeod(Num2); }));
            th2.Start();
            th3 = new Thread(new ThreadStart(delegate { ThMeod(Num3); }));
            th3.Start();
            th4 = new Thread(new ThreadStart(delegate { ThMeod(Num4); }));
            th4.Start();
        }
        private void ThMeod(int Num1)
        {
            Invoke(new Brun(Badd), new object[] { AddNum1 });
            Invoke(new Brun(Badd), new object[] { AddNum2 });
            Invoke(new Brun(Badd), new object[] { AddNum3 });
            Invoke(new Brun(Badd), new object[] { AddNum4 });
        }
        private delegate void Brun(int Num1);
        private void Badd(int Num1)
        {
            timer1.Start(); timer2.Start(); timer3.Start(); timer4.Start();

        }
        private void timer1_Tick(object sender, EventArgs e)
        {
            progressBar1.Value += AddNum1;
            if (progressBar1.Value == 100)
            {
                timer1.Stop();
            }
        }

        private void timer2_Tick(object sender, EventArgs e)
        {
            progressBar2.Value += AddNum2;
            if (progressBar2.Value == 100)
            {
                timer2.Stop();
            }
        }

        private void timer3_Tick(object sender, EventArgs e)
        {
            progressBar3.Value += AddNum3;
            if (progressBar3.Value == 100)
            {
                timer3.Stop();
            }
        }

        private void timer4_Tick(object sender, EventArgs e)
        {
            progressBar4.Value += AddNum4;
            if (progressBar4.Value == 100)
            {
                timer4.Stop();
            }
        }