• 欢迎光临~

多线程实战双色球

开发技术 开发技术 2022-08-30 次浏览

随机数索引生成代码:

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace YpDotNetCoreTaskWinForm.Common
{
    internal class RandomHelper
    {
        public int GetRandomNumberDelay(int min, int max)
        {
            Thread.Sleep(GetRandomNumber(300, 500));
            return GetRandomNumber(min, max);
        }

        private int GetRandomNumber(int min, int max)
        {
            Guid guid=Guid.NewGuid(); //每次生成一个全新的ID
            string sGuid=guid.ToString();
            int seed = DateTime.Now.Millisecond;
            for (int i = 0; i < sGuid.Length; i++)
            {
                switch (sGuid[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 + 3;
                        break;
                    case 'u':
                    case 'v':
                    case 'w':
                    case 'x':
                    case 'y':
                    case 'z':
                        seed = seed + 3;
                        break;
                    default:
                        seed = seed + 4;
                        break;
                }
            }

            Random random=new Random(seed);
            return random.Next(min, max);
        }
    }
}

  具体业务实现代码:

 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using YpDotNetCoreTaskWinForm.Common;

namespace YpDotNetCoreTaskWinForm
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }

        #region 准备初始化的数据
        private string[] RedNumber =
        {
            "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[] BlueNumber = {

            "01","02","03","04","05","06","07","08","09","10",
            "11","12","13","14","15","16"
        };
        #endregion

        // 定义一个锁机制
        private readonly static object lockObject = new object();
        private List<Task> taskList = null;
        private bool isGo = true;

        //开始抽奖
        private void butStart_Click(object sender, EventArgs e)
        {
            taskList=new List<Task>();
            this.butStart.Text = "正在抽奖~";
            this.butStart.Enabled = false;
            this.butStop.Enabled = true;

            foreach (var itemControls in this.groupBox.Controls)
            {
                //判断控件是否为label控件
                if (itemControls is Label)
                {
                    //定义一个公共Lable类型的变量用来存放当前控件实例
                    Label labelControls= (Label)itemControls;
                    //根据label控件的数量开启多线程
                    taskList.Add(Task.Run(() =>
                    {

                        //首先判断蓝红
                        if (labelControls.Name.Contains("Blue"))
                        {
                            //蓝色球
                            while (isGo) //开启一个无限的循环 不断的去取中奖号码
                            {
                                //根据蓝色球的号码池内的数字的数量来获取当前随机的索引值
                                int blueCurrentIndex = new RandomHelper().GetRandomNumberDelay(0, 16);  //
                                //取出当前中奖的号码
                                string blueCurrentNumber = BlueNumber[blueCurrentIndex];

                                ///通过委托让主线程给当前控件附上当前的取出来的号码
                                this.Invoke(new Action(() =>
                                {
                                    labelControls.Text = blueCurrentNumber;
                                }));
                            }
                        }
                        else
                        {
                            while (isGo)
                            {
                                //红色球
                                int redCurrentIndex = new RandomHelper().GetRandomNumberDelay(0, 33);
                                string redCurrentNumber = RedNumber[redCurrentIndex];

                                //将锁放到这里是可以避免出现重复数字,
                                //如果不放到这里的话,会导致后续线程获取到号码集合与上个线程获取到的号码集合是一样的,
                                //就会导致出现重复的号码。
                                lock (lockObject)
                                {
                                    var currentControlsTextValueList = GetCurrentRedLabelTextValueList();
                                    if (!currentControlsTextValueList.Contains(redCurrentNumber))
                                    {
                                        this.Invoke(new Action(() =>
                                        {
                                            labelControls.Text = redCurrentNumber;
                                        }));
                                    }
                                }
                            }
                        }

                    }));
                }
            }

            TaskFactory taskFactory=new TaskFactory();
            taskFactory.ContinueWhenAll(taskList.ToArray(), ts =>
            {
                ShowWinningNumberMessg();
                this.butStart.Text = "Start";
                this.butStart.Enabled = true;
                this.butStop.Enabled = false;
                this.isGo = true;
                
            });
        }

        private void ShowWinningNumberMessg()
        {
            MessageBox.Show($"本期双色球中奖结果为:红球" +
                $"{this.labRed1.Text}," +
                $"{this.labRed2.Text}," +
                $"{this.labRed3.Text}," +
                $"{this.labRed4.Text}," +
                $"{this.labRed5.Text}," +
                $"{this.labRed6.Text}," +
                $"蓝球号码" +
                $"{this.labBlue.Text}");
        }

        private List<string> GetCurrentRedLabelTextValueList()
        {
            List<string> redLabelTextValueList=new List<string>();
            foreach (var labelControls in this.groupBox.Controls)
            {
                if (labelControls is Label && ((Label)labelControls).Name.Contains("Red")){

                    redLabelTextValueList.Add(((Label)labelControls).Text);
                }
            }

            if (redLabelTextValueList.Count(c => c.Contains("00")) == 0 && redLabelTextValueList.Distinct().Count() < 6)
            {
                Debug.WriteLine("出现重复号码!");
                foreach (var item in redLabelTextValueList)
                {
                    Debug.WriteLine(item);
                }
            }
            return redLabelTextValueList;

        }

        //结束抽奖
        private void butStop_Click(object sender, EventArgs e)
        {
            this.isGo = false;
        }
    }
}

  

 

程序员灯塔
转载请注明原文链接:多线程实战双色球
喜欢 (0)