模拟双色球摇奖程序

来源:互联网 发布:浙江大学网络缴费平台 编辑:程序博客网 时间:2024/04/30 01:54

双色球规则:前6个红球抽出1-33之间的任意的数,不能重复,第7个蓝球抽出1-16之间的任意一个数。

界面很简单,7个Label标签搞定,1个开始Button,点击后Button上文本改为"停止",下面显示中奖结果!!

思考本程序如果在界面上不停地给Label赋值,会把界面UI层搞死,因此,开启另外一个线程对Label标签不停地赋值是必须的。在实际开发过程中,如果手工自己new一个Thread,并开启,这期间必然会有效率和性能问题,因此也不太适合,利用Microsoft为我们提供的线程池技术势必大大提高我们的程序性能。

将方法排入队列以便执行。此方法在有线程池线程变得可用时执行ThreadPool.QueueUserWorkItem()

public partial class DoubleBall2Frm : Form    {        private List<Label> _listLb;        private bool flag = false;        public DoubleBall2Frm()        {            InitializeComponent();            _listLb = new List<Label>() { lb1, lb2, lb3, lb4, lb5, lb6, lb7, lbResult };        }        //为Label赋值的方法        private void SetLabelValue(Label lb, string value)        {            lb.Text = value;        }        private void btnStart_Click(object sender, EventArgs e)        {            if (flag)            {                flag = false;                btnStart.Text = "开始";                lbResult.Visible = true;            }            else            {                flag = true;                btnStart.Text = "停止";                lbResult.Visible = false;                //开启线程为界面上的 摇奖Label赋值                ThreadPool.QueueUserWorkItem(obj =>                {                    Random rdom = new Random();                    while (flag)//实现滚动                    {                        #region 生成6个1-33之间不同的数字                        //生成6个1-33之间不同的数字                        HashSet<int> hs = new HashSet<int>();                        while (true)                        {                            if (hs.Count == 6)                            {                                break;                            }                            hs.Add(rdom.Next(1, 34));                        }                        #endregion                        #region 对界面上的Label标签分别赋值,不足10的数字前面补0                        int[] arr = hs.ToArray();                        //对前6个Label赋值,6个红球                        for (int i = 0; i < 6; i++)                        {                            if (_listLb[i].InvokeRequired)                            {                                //学会利用泛型委托Action                                Invoke(new Action<Label, string>(SetLabelValue), _listLb[i], arr[i] < 10 ? "0" + arr[i] : arr[i].ToString());                            }                            else                            {                                _listLb[i].Text = arr[i] < 10 ? "0" + arr[i] : arr[i].ToString();                            }                        }                        //第7个蓝球,生成1-16之间的一个数字                        int numStr = rdom.Next(1, 17);                        if (_listLb[6].InvokeRequired)                        {                            //学会利用泛型委托Action                            Invoke(new Action<Label, string>(SetLabelValue), _listLb[6], numStr < 10 ? "0" + numStr : numStr.ToString());                        }                        else                        {                            _listLb[6].Text = numStr < 10 ? "0" + numStr : numStr.ToString();                        }                        //第8个结果显示                        if (_listLb[7].InvokeRequired)                        {                            Invoke(new Action<Label, string>(SetLabelValue), _listLb[7], string.Format("中奖号码为:{0}  {1}  {2}  {3}  {4}  {5}  {6}", lb1.Text, lb2.Text, lb3.Text, lb4.Text, lb5.Text, lb6.Text, lb7.Text));                        }                        else                        {                            _listLb[7].Text = string.Format("中奖号码为:{0}  {1}  {2}  {3}  {4}  {5}  {6}", lb1.Text, lb2.Text, lb3.Text, lb4.Text, lb5.Text, lb6.Text, lb7.Text);                        }                        #endregion                        Thread.Sleep(200);//停一小会儿,不严重消耗cpu                    }                });            }        }        private void DoubleBall2Frm_FormClosing(object sender, FormClosingEventArgs e)        {            //关闭前必须停止摇奖,否则报错            if (btnStart.Text == "停止")            {                e.Cancel = true;                MessageBox.Show("请先停止摇奖");            }        }    }

 

 

 

 

原创粉丝点击