首页 > 代码库 > C# 摇奖机实例(线程)
C# 摇奖机实例(线程)
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); } } }}
C# 摇奖机实例(线程)
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。