开始画面
抽奖中:
抽奖结果:
需要一个随机数Random的帮助类,让随机数唯一性
public class RandomHelper { public int GetNum(int min, int max) { Thread.Sleep(1000);//随机休息1秒 return GetIntNum(min,max); } ////// 获取随机数,解决重复问题 /// /// 返回的随机数字包含下限 /// 返回的随机数字不包含上限 /// private int GetIntNum(int min,int max) { Guid Guidnum = Guid.NewGuid(); string guid = Guidnum.ToString(); int seed = DateTime.Now.Millisecond; for (int i = 0; i < guid.Length; i++) { switch (guid[i]) { case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': case 'g': seed = seed + 1; break; case 'h': case 'i': case 'j': case 'k': case 'l': case 'm': case 'n': seed = seed + 2; break; case 'o': case 'p': case 'q': case 'r': case 's': case 't': seed = seed + seed; break; case 'u': case 'v': case 'w': case 'x': case 'y': case 'z': seed = seed + 3; break; default: seed = seed + 4; break; } } return new Random(seed).Next(min,max); }
Form的 代码编写
先要放2个
public Form1() { InitializeComponent(); IsBegin = true; this.btnEnd.Enabled = false; } ////// 标识是否开始摇奖 /// private static bool IsBegin = true; private string[] RedNum = { "01","02","03","04","05","06","07","08","09","10","11","12","13", "14","15","16","17","18","19","20","21","22","23","24","25","26", "27","28","29","30","31","32","33" }; private string[] BuleNum = { "01","02","03","04","05","06","07","08","09","10","11","12","13", "14","15","16" }; private void btnStart_Click(object sender, EventArgs e) { TaskFactory taskFactory = new TaskFactory(); List taskList = new List (); IsBegin = true; this.btnStart.Enabled = false; // Thread.Sleep(1000); this.btnEnd.Enabled = true; foreach (Control item in gBox.Controls) { if (item is Label) { Label lbl = (Label)item; taskList.Add(taskFactory.StartNew( () => { while (IsBegin) { this.UpdateNum(lbl); } })); } } taskFactory.ContinueWhenAll(taskList.ToArray(),tList=>this.ShowNumber());//等所有线程操作完毕后才显示中奖号。 } /// /// 锁 /// private static readonly object Num_Lock = new object(); private void UpdateNum(Label lbl) { RandomHelper randomHelper = new RandomHelper(); if (lbl.Name.Contains("Blue")) { int num = randomHelper.GetNum(0,16); string blueText = BuleNum[num]; this.UpdateLbl(lbl, blueText); } else { int num = randomHelper.GetNum(0, 33); string redText = RedNum[num]; lock (Num_Lock) { List<string> list = this.GetLabelList(); if (list.Contains(redText)) { return; } else { this.UpdateLbl(lbl,redText); } } } } /// /// 获取当前已经抽出的双色球,防止重复 /// /// 所有控件的值 private List<string> GetLabelList() { List<string> strList = new List<string>(); foreach (Control item in gBox.Controls) { if (item is Label) { Label label = (Label)item; strList.Add(label.Text); } } return strList; } /// /// 通过主线程修改UI /// /// 修改的Label /// 修改的值 private void UpdateLbl(Label lbl, string text) { if (lbl.InvokeRequired) { this.Invoke(new Action(() => { lbl.Text = text; //Thread.Sleep(2000); //Console.WriteLine($"当前UpdateLbl线程id{Thread.CurrentThread.ManagedThreadId}"); }));//交给UI线程去更新 } else { lbl.Text = text; } } /// /// 结束抽奖,开奖 /// /// /// private void btnEnd_Click(object sender, EventArgs e) { if (!this.GetLabelList().Contains("00")) { IsBegin = false; this.btnEnd.Enabled = false; this.btnStart.Enabled = true; } else { MessageBox.Show("请慢一点,稍后再试"); } } private void ShowNumber() { MessageBox.Show(string.Format("本期双色球结果是 {0} {1} {2} {3} {4} {5} {6}", lbRed1.Text, lbRed2.Text, lbRed3.Text, lbRed4.Text, lbRed5.Text, lbRed6.Text, lbBlue.Text)); } }