首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

C#线程有关问题 急死小弟我啦 帮帮忙- -

2011-12-30 
C#线程问题 急死我啦 帮帮忙- -!这个是我做的摇奖机的一个东东,我这样写的线程没有问题。但是我发现点击了

C#线程问题 急死我啦 帮帮忙- -!
这个是我做的摇奖机的一个东东,我这样写的线程没有问题。但是我发现点击了停止按钮(STOP启动按钮事件那里),这些显示的9个数字都是一模一样的,我再点击开始(Start启动按钮事件那里)。他就报错,大家帮帮忙 ,偶郁闷啦
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 摇奖机
{
  public partial class ERNIE : Form
  {
  private Point mouseOffset;//定义一个变量,用于存放鼠标的坐标
  private bool isMouseDown = false;//定一个布尔变量,用于判断鼠标状态
  //Thread[] th = new Thread[9];
  ThreadStart t;
  Thread th1;
  Thread th2;
  Thread th3;
  Thread th4;
  Thread th5;
  Thread th6;
  Thread th7;
  Thread th8;
  Thread th9;
  public ERNIE()
  {
  InitializeComponent();
  th1 = new Thread(new ThreadStart(rnd1));
  th2 = new Thread(new ThreadStart(rnd2));
  th3 = new Thread(new ThreadStart(rnd3));
  th4 = new Thread(new ThreadStart(rnd4));
  th5 = new Thread(new ThreadStart(rnd5));
  th6 = new Thread(new ThreadStart(rnd6));
  th7 = new Thread(new ThreadStart(rnd7));
  th8 = new Thread(new ThreadStart(rnd8));
  th9 = new Thread(new ThreadStart(rnd9));
  //for (int i = 0; i < 9; i++)
  //{
  //th[i] = new Thread(t);
  // }
  }
//===============================================Start启动按钮事件=======================================
  public void BTstart_Click(object sender, EventArgs e)
  {
  ERNIE.CheckForIllegalCrossThreadCalls = false;
  th1.Start();
  th2.Start();
  th3.Start();
  th4.Start();
  th5.Start();
  th6.Start();
  th7.Start();
  th8.Start();
  th9.Start();
  }
//===============================================STOP启动按钮事件=======================================
  private void button1_Click(object sender, EventArgs e)
  {
  th1.Abort();
  th2.Abort();
  th3.Abort();
  th4.Abort();
  th5.Abort();
  th6.Abort();
  th7.Abort();
  th8.Abort();
  th9.Abort();
  LB10.Text = LB1.Text;
  LB11.Text = LB2.Text;
  LB12.Text = LB3.Text;
  LB13.Text = LB4.Text;
  LB14.Text = LB5.Text;
  LB15.Text = LB6.Text;
  LB16.Text = LB7.Text;
  LB17.Text = LB8.Text;
  LB18.Text = LB9.Text;
  }
//===============================================线程控制随机数==========================================
  public void rnd1()
  {
  Random r = new Random();
  int x = r.Next(0,10);
  LB1.Text=x.ToString();
  Thread.Sleep(10);
  rnd1();
  }
  public void rnd2()
  {
  Random r = new Random();
  int x = r.Next(0,10);
  LB2.Text = x.ToString();
  Thread.Sleep(10);
  rnd2();
  }
  public void rnd3()


  {
  Random r = new Random();
  int x = r.Next(0,10);
  LB3.Text = x.ToString();
  Thread.Sleep(10);
  rnd3();
  }
  public void rnd4()
  {
  Random r = new Random();
  int x = r.Next(0,10);
  LB4.Text = x.ToString();
  Thread.Sleep(10);
  rnd4();
  }
  public void rnd5()
  {
  Random r = new Random();
  int x = r.Next(0,10);
  LB5.Text = x.ToString();
  Thread.Sleep(10);
  rnd5();
  }
  public void rnd6()
  {
  Random r = new Random();
  int x = r.Next(0,10);
  LB6.Text = x.ToString();
  Thread.Sleep(10);
  rnd6();
  }
  public void rnd7()
  {
  Random r = new Random();
  int x = r.Next(0,10);
  LB7.Text = x.ToString();
  Thread.Sleep(10);
  rnd7();
  }
  public void rnd8()
  {
  Random r = new Random();
  int x = r.Next(0,10);
  LB8.Text = x.ToString();
  Thread.Sleep(10);
  rnd8();
  }
  public void rnd9()
  {
  Random r = new Random();
  int x = r.Next(0,10);
  LB9.Text = x.ToString();
  Thread.Sleep(10);
  rnd9();
  }

//============================================鼠标移动状态事件===========================================
  private void ERNIE_MouseDown(object sender, MouseEventArgs e)//鼠标按下事件
  {
  if (e.Button == MouseButtons.Left)//如果鼠标点击的为左键
  {
  mouseOffset = new Point(-e.X, -e.Y);//得到鼠标当前位置坐标赋值于mouseoffset
  isMouseDown = true;//鼠标状态为真
  }
  }

  private void ERNIE_MouseMove(object sender, MouseEventArgs e)//鼠标移动事件
  {
  if (isMouseDown)//如果鼠标的状态为真
  {
  Point mousePos = Control.MousePosition;//鼠标移动变量赋值于mousePos
  mousePos.Offset(mouseOffset.X, mouseOffset.Y);//鼠标当前位置变量赋值于mousePos
  this.Location = mousePos;//窗口跟着做相应的移动
  }
  }

  private void ERNIE_MouseUp(object sender, MouseEventArgs e)//鼠标松开时间
  {
  if (e.Button == MouseButtons.Left)//如果鼠标松开的为左键
  {
  isMouseDown = false;//则鼠标状态为假
  }
  }
  }
}

[解决办法]
你的线程是不能Restart的, 如果你要重新运行这些方法,你只能重新创建线程。好像错误提示就是Thread cannot restart. 诸如此类的话,你应该重新创建线程,或者你可以用.Net本身的线程池。

热点排行