(精华)2020年8月13日 C#基础知识点 windform实现双色球

public partial class frmSSQ : Form
    {
        public frmSSQ()
        {
            InitializeComponent();
            this.btnStart.Enabled = true;
            this.btnStop.Enabled = false;
        }

        #region Data 
        /// 
        /// 红球集合  其实可以写入配置文件
        /// 
        private string[] RedNums =
        {
            "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[] BlueNums =
        {
            "01","02","03","04","05","06","07","08","09","10",
            "11","12","13","14","15","16"
        };

        private bool IsGoOn = true;
        private List<Task> taskList = new List<Task>();

        #endregion

        #region UI
        /// 
        /// 点击开始:
        /// 
        /// 
        /// 
        private void btnStart_Click(object sender, EventArgs e)
        {
            try
            {
                #region 初始化动作
                this.btnStart.Text = "运行ing";
                this.btnStart.Enabled = false;
                this.IsGoOn = true;
                this.taskList = new List<Task>();
                this.lblBlue.Text = "00";
                this.lblRed1.Text = "00";
                this.lblRed2.Text = "00";
                this.lblRed3.Text = "00";
                this.lblRed4.Text = "00";
                this.lblRed5.Text = "00";
                this.lblRed6.Text = "00";
                #endregion
                Thread.Sleep(1000);
                foreach (var control in this.gboSSQ.Controls)
                {
                    if (control is Label)
                    {
                        Label label = (Label)control;
                        if (label.Name.Contains("Blue"))
                        {
                            taskList.Add(Task.Run(() =>
                            {
                                try
                                {
                                    while (IsGoOn)
                                    {
                                        //1 获取随机数 
                                        int index = new RandomHelper().GetRandomNumberDelay(0, 16);
                                        string sNumber = this.BlueNums[index];
                                        //2 更新界面
                                        //this.lblBlue.Text = sNumber;
                                        //子线程不能操作控件,委托给主线程操作
                                        //this.Invoke
                                        this.Invoke(new Action(() =>
                                        {
                                            label.Text = sNumber;
                                        }));
                                        //3 循环
                                    }
                                }
                                catch (Exception ex)
                                {
                                    Console.WriteLine(ex.Message);
                                }
                            }));
                        }
                        else if (label.Name.Contains("Red"))
                        {
                            taskList.Add(Task.Run(() =>
                            {
                                try
                                {
                                    while (IsGoOn)
                                    {
                                        int index = new RandomHelper().GetRandomNumberDelay(0, 33);
                                        string sNumber = this.RedNums[index];
                                        //可能重复  得去重
                                        //1 过河的卒子  一次性拿6个,一次更新,但是界面更新有规律
                                        //2 再来个数组标记一下,有点笨拙;其实界面不就有吗?
                                        //3 80奋斗  数据分隔,安全高效,但是有规律

                                        //检测下是否重复,  直接比对界面
                                        //获取随机   比对界面   更新
                                        lock (SSQ_Lock)
                                        {
                                            List<string> usedNumberList = this.GetUsedRedNumbers();
                                            if (!usedNumberList.Contains(sNumber))
                                            {
                                                this.Invoke(new Action(() =>
                                                 {
                                                     //Thread.Sleep(10);
                                                     label.Text = sNumber;
                                                 }));//实际上是同步的
                                            }
                                        }
                                    }
                                }
                                catch (Exception ex)
                                {
                                    Console.WriteLine(ex.Message);
                                }
                            }));
                        }
                    }
                }
                Task.Factory.ContinueWhenAll(this.taskList.ToArray(), tArray =>
                {
                    this.ShowResult();
                    this.Invoke(new Action(() =>
                    {
                        this.btnStart.Enabled = true;
                        this.btnStop.Enabled = false;
                    }));
                });
                Task.Delay(10 * 1000).ContinueWith(t =>
                {
                    this.Invoke(new Action(() =>
                    {
                        this.btnStop.Enabled = true;
                    }));
                });

            }
            catch (Exception ex)
            {
                Console.WriteLine("双色球启动出现异常:{0}", ex.Message);
            }
        }

        private static readonly object SSQ_Lock = new object();

        private List<string> GetUsedRedNumbers()
        {
            List<string> usedNumberList = new List<string>();
            foreach (var controler in this.gboSSQ.Controls)
            {
                if (controler is Label)
                {
                    if (((Label)controler).Name.Contains("Red"))
                    {
                        usedNumberList.Add(((Label)controler).Text);
                    }
                }
            }
            return usedNumberList;
        }

        /// 
        /// 点击结束
        /// 
        /// 
        /// 
        private void btnStop_Click(object sender, EventArgs e)
        {
            this.IsGoOn = false;
        }

        /// 
        /// 弹框提示数据
        /// 
        private void ShowResult()
        {
            MessageBox.Show(string.Format("本期双色球结果为:{0} {1} {2} {3} {4} {5}  蓝球{6}"
                , this.lblRed1.Text
                , this.lblRed2.Text
                , this.lblRed3.Text
                , this.lblRed4.Text
                , this.lblRed5.Text
                , this.lblRed6.Text
                , this.lblBlue.Text));
        }
        #endregion


        #region PrivateMethod

        #endregion
    }

你可能感兴趣的:(C#,c#)