让面板数字不停变化 我是先定义一个全局的布尔变量flag等于ture,然后while(flag){//让数字显示在面板上的方法...},点击停止的时候再将flag设成flase
不知道又没有更好的方法让数字在面板一直变动的方法,总觉得我这个很不专业
namespace 多线程1
{
public partial class Form1 : Form
{
bool flag = true;
public Form1()
{
InitializeComponent();
drn = new dgrunNumber(runNumber);
}
private void btnStar_Click(object sender, EventArgs e)
{
Thread thread = new Thread(run);
thread.IsBackground = true;
thread.Start();
}
private void run()
{
Random rd = new Random();
while (flag)
{
int i = rd.Next(10);
this.Invoke(drn, i.ToString());
}
}
private void runNumber(string msg)
{
lab1.Text = msg;
}
delegate void dgrunNumber(string msg);
dgrunNumber drn = null;
private void btnStop_Click(object sender, EventArgs e)
{
flag = false;
}
}
}
|