我照着书上的教程写了个 用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; }
}
}
}
下面是偶的代码
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; }
}
}
}
解决方案 »
- C#通过按钮切换选项卡
- 添加Access的问题,在线等,急
- 请教一个VS2010中的Dotfuscator Software Services工具的问题
- 关于后台返回 一个list<>集合怎么把数据传送JS进行循环输出
- 请教一个使用SqlCommandBuilder更新数据库的问题
- 我想写一个监控程序,如有一个程序a我让它一直开着,,写个程序b,当a出现异常时(有弹出框)把a重启,怎么实现
- C#中的ListView添加记录时会引发ItemChecked事件,怎么办?
- 有没有能够看到NET命名空间中的类的方法的具体代码的办法?
- 关于在文件指定的位置写文件
- 请问高手,怎样用网络传送DataTable??
- 用户永远都是笨蛋吗
- 求助,关于datatable的问题
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();
}
}