我写了一个摇奖机的程序,可是在我运行时点击恢复按钮时代码有时没有被执行,但是有时候却又被执行了,这是为什么啊??代码如下:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Threading;
namespace WindowsApplication8
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.Label label4;
private System.Windows.Forms.Label label5;
private System.Windows.Forms.Label label6;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Button button3;
private System.Windows.Forms.Button button4;
private System.Threading.Thread thread1;
private System.Threading.Thread thread2;
private System.Threading.Thread thread3;
private System.Threading.Thread thread4;
private System.Threading.Thread thread5;
private System.Threading.Thread thread6;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(Form1));
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.label3 = new System.Windows.Forms.Label();
this.label4 = new System.Windows.Forms.Label();
this.label5 = new System.Windows.Forms.Label();
this.label6 = new System.Windows.Forms.Label();
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.button3 = new System.Windows.Forms.Button();
this.button4 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// label1
//
this.label1.Location = new System.Drawing.Point(16, 80);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(40, 40);
this.label1.TabIndex = 0;
//
// label2
//
this.label2.Location = new System.Drawing.Point(80, 80);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(40, 40);
this.label2.TabIndex = 1;
//
// label3
//
this.label3.Location = new System.Drawing.Point(144, 80);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(40, 40);
this.label3.TabIndex = 2;
//
// label4
//
this.label4.Location = new System.Drawing.Point(208, 80);
this.label4.Name = "label4";
this.label4.Size = new System.Drawing.Size(40, 40);
this.label4.TabIndex = 3;
//
// label5
//
this.label5.Location = new System.Drawing.Point(272, 80);
this.label5.Name = "label5";
this.label5.Size = new System.Drawing.Size(40, 40);
this.label5.TabIndex = 4;
//
// label6
//
this.label6.Location = new System.Drawing.Point(344, 80);
this.label6.Name = "label6";
this.label6.Size = new System.Drawing.Size(40, 40);
this.label6.TabIndex = 5;
//
// button1
//
this.button1.Location = new System.Drawing.Point(32, 208);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(56, 32);
this.button1.TabIndex = 6;
this.button1.Text = "开始";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(240, 208);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(56, 32);
this.button2.TabIndex = 7;
this.button2.Text = "恢复";
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// button3
//
this.button3.Location = new System.Drawing.Point(336, 208);
this.button3.Name = "button3";
this.button3.Size = new System.Drawing.Size(56, 32);
this.button3.TabIndex = 8;
this.button3.Text = "退出";
this.button3.Click += new System.EventHandler(this.button3_Click);
//
// button4
//
this.button4.Location = new System.Drawing.Point(136, 208);
this.button4.Name = "button4";
this.button4.Size = new System.Drawing.Size(64, 32);
this.button4.TabIndex = 9;
this.button4.Text = "暂停";
this.button4.Click += new System.EventHandler(this.button4_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("$this.BackgroundImage")));
this.ClientSize = new System.Drawing.Size(432, 294);
this.Controls.Add(this.button4);
this.Controls.Add(this.button3);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Controls.Add(this.label6);
this.Controls.Add(this.label5);
this.Controls.Add(this.label4);
this.Controls.Add(this.label3);
this.Controls.Add(this.label2);
this.Controls.Add(this.label1);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.Fixed3D;
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void button1_Click(object sender, System.EventArgs e)
{
thread1 = new Thread(new ThreadStart(run1));
thread2 = new Thread(new ThreadStart(run2));
thread3 = new Thread(new ThreadStart(run3));
thread4 = new Thread(new ThreadStart(run4));
thread5 = new Thread(new ThreadStart(run5));
thread6 = new Thread(new ThreadStart(run6));
thread1.Start();
thread2.Start();
thread3.Start();
thread4.Start();
thread5.Start();
thread6.Start();
}
public void run1()
{
Random one = new Random(1);
do
{
int oneThread = one.Next(10);
this.label1.Text = oneThread.ToString();
Thread.Sleep(10);
}
while(true);
}
public void run2()
{
Random two = new Random(2);
do
{
int twoThread = two.Next(10);
this.label2.Text = twoThread.ToString();
Thread.Sleep(10);
}
while(true);
}
public void run3()
{
Random three = new Random(3);
do
{
int threeThread = three.Next(10);
this.label3.Text = threeThread.ToString();
Thread.Sleep(10);
}
while(true);
}
public void run4()
{
Random four = new Random(4);
do
{
int foureThread = four.Next(10);
this.label4.Text = foureThread.ToString();
Thread.Sleep(10);
}
while(true);
}
public void run5()
{
Random five = new Random(5);
do
{
int fiveThread = five.Next(10);
this.label5.Text = fiveThread.ToString();
Thread.Sleep(10);
}
while(true);
}
public void run6()
{
Random six = new Random(6);
do
{
int sixThread = six.Next(10);
this.label6.Text = sixThread.ToString();
Thread.Sleep(10);
}
while(true);
}
private void button3_Click(object sender, System.EventArgs e)
{
try
{
thread1.Abort();
thread2.Abort();
thread3.Abort();
thread4.Abort();
thread5.Abort();
thread6.Abort();
Application.Exit();
}
catch (Exception te)
{
MessageBox.Show(te.Message);
}
}
private void button2_Click(object sender, System.EventArgs e)
{
try
{
if(thread1.ThreadState == ThreadState.Suspended || thread2.ThreadState == ThreadState.Suspended ||
thread3.ThreadState == ThreadState.Suspended || thread4.ThreadState == ThreadState.Suspended ||
thread5.ThreadState == ThreadState.Suspended || thread6.ThreadState == ThreadState.Suspended ||
thread1.ThreadState == ThreadState.WaitSleepJoin || thread2.ThreadState == ThreadState.WaitSleepJoin ||
thread3.ThreadState == ThreadState.WaitSleepJoin || thread4.ThreadState == ThreadState.WaitSleepJoin ||
thread5.ThreadState == ThreadState.WaitSleepJoin || thread6.ThreadState == ThreadState.WaitSleepJoin ||
thread1.ThreadState == ThreadState.SuspendRequested || thread2.ThreadState == ThreadState.SuspendRequested ||
thread3.ThreadState == ThreadState.SuspendRequested || thread4.ThreadState == ThreadState.SuspendRequested ||
thread5.ThreadState == ThreadState.SuspendRequested || thread6.ThreadState == ThreadState.SuspendRequested)
{
thread1.Resume();
thread2.Resume();
thread3.Resume();
thread4.Resume();
thread5.Resume();
thread6.Resume();
}
}
catch (Exception te)
{
MessageBox.Show(te.Message);
}
}
private void button4_Click(object sender, System.EventArgs e)
{
try
{
if(thread1.ThreadState == ThreadState.Running || thread2.ThreadState == ThreadState.Running ||
thread3.ThreadState == ThreadState.Running || thread4.ThreadState == ThreadState.Running ||
thread5.ThreadState == ThreadState.Running || thread6.ThreadState == ThreadState.Running ||
thread1.ThreadState == ThreadState.WaitSleepJoin || thread2.ThreadState == ThreadState.WaitSleepJoin ||
thread3.ThreadState == ThreadState.WaitSleepJoin || thread4.ThreadState == ThreadState.WaitSleepJoin ||
thread5.ThreadState == ThreadState.WaitSleepJoin || thread6.ThreadState == ThreadState.WaitSleepJoin)
{
thread1.Suspend();
thread2.Suspend();
thread3.Suspend();
thread4.Suspend();
thread5.Suspend();
thread6.Suspend();
}
}
catch (Exception te)
{
MessageBox.Show(te.Message);
}
}
}
}