using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
namespace yaojiangji
{
public partial class Form1 : Form
{
List<Label> lbList = new List<Label>();
bool isCreate = false;
public Form1()
{
InitializeComponent();
Control.CheckForIllegalCrossThreadCalls = false;
}
private void btnStart_Click(object sender, EventArgs e)
{
if (isCreate)
{
isCreate = false;
this.btnStart.Text = "开始";
}
else
{
isCreate = true;
this.btnStart.Text = "结束";
//方法一:
/*
new Thread(
()=>
{
Random random = new Random();
while (isCreate)
{
for (int i = 0; i < 6; i++)
{
lbList[i].Text = random.Next(1, 10).ToString();
}
Thread.Sleep(200);
}
}
).Start();
*/
//方法二:
Thread thread = new Thread(new ThreadStart(start));
//设置后台线程
thread.IsBackground = true;
thread.Start();
}
}
private void Form1_Load(object sender, EventArgs e)
{
for (int i = 0; i < 6; i++)
{
Label label = new Label();
label.Text = i.ToString();
label.AutoSize = true;
label.Location = new Point(50*i+15,50);
lbList.Add(label);
this.Controls.Add(label);
}
}
public void start()
{
Random random = new Random();
while (isCreate)
{
for (int i = 0; i < 6; i++)
{
lbList[i].Text = random.Next(1, 10).ToString();
}
Thread.Sleep(200);
}
}
}
}