抽奖小程式
开始抽奖状态
结束抽奖状态
using System; using System.Collections.Generic; using System.ComponentModel; using System.Configuration; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; using System.Windows.Forms; namespace WindowsFormsApp1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } string[] participantList; bool isRunning = false; private void button1_Click(object sender, EventArgs e) { if (!isRunning) { isRunning = true; button1.Text = "停止"; } else { isRunning = false; button1.Text = "开始"; } Thread thread = new Thread(Run); thread.IsBackground = true;//设置为后台线程 thread.Start(); } public void Run() { while (isRunning) { Random random = new Random(); var index = random.Next(0, participantList.Length - 1); label3.Text = participantList[index]; } } private void Form1_Load(object sender, EventArgs e) { Control.CheckForIllegalCrossThreadCalls = false;//允许跨线程访问控件 participantList = ConfigurationManager.AppSettings["participant"].Split(','); } } }App.config
<?xml version="1.0" encoding="utf-8" ?> <configuration> <startup> <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.8" /> </startup> <appSettings> <!--参与人员名单--> <add key="participant" value="张三,李四,王五"/> </appSettings> </configuration>