如图我用了7个lable
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Threading;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- namespace 摇奖机
- {
- public partial class Form1 : Form
- {
- public Form1()
- {
- InitializeComponent();
- }
- Thread th;
- bool b = false;//控制调用方法的条件
- private void button1_Click(object sender, EventArgs e)
- {
- if (button1.Text == "开始")//点击按钮后,如果是开始
- {
- b = true;
- button1.Text = "暂停";
- th = new Thread(PlayGames);
- th.IsBackground = true;//后台线程
- th.Start();
- }
- else//如果是暂停
- {
- b = false;
- button1.Text = "开始";
- }
- }
- void PlayGames()
- {
- Random r = new Random();
- while (b)
- {
- label1.Text = r.Next(0, 10).ToString();
- label2.Text = r.Next(0, 10).ToString();
- label3.Text = r.Next(0, 10).ToString();
- label4.Text = r.Next(0, 10).ToString();
- label5.Text = r.Next(0, 10).ToString();
- label6.Text = r.Next(0, 10).ToString();
- label7.Text = r.Next(0, 10).ToString();
- }
- }
- private void Form1_Load(object sender, EventArgs e)
- {
- Control.CheckForIllegalCrossThreadCalls = false;//关闭检查跨线程访问
- }
- private void Form1_FormClosing(object sender, FormClosingEventArgs e)
- {
- if (th != null)
- {
- th.Abort();//主窗体关闭后,关闭线程
- }
- }
- }
- }
复制代码 |
|