我照着书上的教程写了个 用7个线程控制7个lbl显示数字,运行的时候提示说我label的值不是由其线程赋值的,不知道咋解决看过了原书附带的源代码,运行起来同样的问题。
下面是偶的代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;namespace Lottery
{
    public partial class Playball : Form
    {
        public Playball()
        {
            InitializeComponent();
        }        Thread playBallThread = null;        private void Playball_FormClosed(object sender, FormClosedEventArgs e)
        {
            if (playBallThread != null)
                playBallThread.Abort();
        }        private void btnlottery_Click(object sender, EventArgs e)
        {
            this.playBallThread = new Thread(new ThreadStart(this.BallThreadProc));
            this.playBallThread.Start();
        }
        private void BallThreadProc()
        {
            Ball B1 = new Ball(lblRed1,Ball.BallType.Red);
            Thread T1 = new Thread(new ThreadStart(B1.Run));
            T1.Start();
            B1.Wait();
            Ball B2 = new Ball(lblRed2, Ball.BallType.Red);
            Thread T2 = new Thread(new ThreadStart(B2.Run));
            T2.Start();
            B2.Wait();
            Ball B3 = new Ball(lblRed3, Ball.BallType.Red);
            Thread T3 = new Thread(new ThreadStart(B3.Run));
            T3.Start();
            B3.Wait();
            Ball B4 = new Ball(lblRed4, Ball.BallType.Red);
            Thread T4 = new Thread(new ThreadStart(B4.Run));
            T4.Start();
            B4.Wait();
            Ball B5 = new Ball(lblRed5, Ball.BallType.Red);
            Thread T5 = new Thread(new ThreadStart(B5.Run));
            T5.Start();
            B5.Wait();
            Ball B6 = new Ball(lblRed6, Ball.BallType.Red);
            Thread T6 = new Thread(new ThreadStart(B6.Run));
            T6.Start();
            B6.Wait();
            Ball B7 = new Ball(lblBule, Ball.BallType.Blue);
            Thread T7 = new Thread(new ThreadStart(B7.Run));
            T7.Start();
            B7.Wait();            this.lblMessage.Text = "Winning Lottery Number:" + B1.Value + B2.Value + B3.Value + B4.Value + B5.Value + B6.Value + B7.Value;
        }
        
    }
}using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Threading;namespace Lottery
{
    public class Ball
    {
        Random r = new Random((int)DateTime.Now.Ticks);
        AutoResetEvent ballEvent = new AutoResetEvent(false);
        Label curLabel = null;
        BallType type;
        public enum BallType { Red, Blue }        public Ball(Label ctl, BallType t)
        {
            curLabel = ctl;
            type = t;
        }        public void Run()
        {
            for (int i = 0; i < 50; i++)
            {
                if (type == BallType.Red)
                    curLabel.Text = r.Next(1, 33).ToString();
                else
                {
                    curLabel.Text = r.Next(1, 16).ToString();
                    Thread.Sleep(100);
                }
                ballEvent.Set();
            }
        }        public void Wait()
        {
            ballEvent.WaitOne();
        }
        public string Value
        {
            get { return "" + curLabel.Text; }
        }
    }
}

解决方案 »

  1.   


    public void Run()
            {
                for (int i = 0; i < 50; i++)
                {
                    if (type == BallType.Red)
                        curLabel.Text = r.Next(1, 33).ToString();
                    else
                    {
                        curLabel.Text = r.Next(1, 16).ToString();
                        Thread.Sleep(100);
                    }
                    ballEvent.Set();
                }
            }控件操作处需要使用委托实现,我是这样改的,不过出现了界面锁,你看看算法有没有死锁了。public delegate void setLbl(Label lbl, string str);            public void SetLbl(Label lbl, string str)
                {
                    try
                    {
                        if (lbl.InvokeRequired)
                        {
                            lbl.Parent.Invoke(new setLbl(SetLbl), lbl, str);
                        }
                        else
                        {
                            lbl.Text = str;
                        }
                    }
                    catch (Exception e)
                    {                }
                }                            public void Run()
                {
                    for (int i = 0; i < 50; i++)
                    {
                        if (type == BallType.Red)
                            //curLabel.Text = r.Next(1, 33).ToString();
                            SetLbl(curLabel, r.Next(1, 33).ToString());
                        else
                        {
                            //curLabel.Text = r.Next(1, 16).ToString();
                            SetLbl(curLabel, r.Next(1, 16).ToString());
                            Thread.Sleep(100);
                        }
                        //ballEvent.Set();
                    }
                }
      

  2.   

    你书本的是vs2003的吧?你用的是vs2005吧