其他分享
首页 > 其他分享> > 多线程实战双色球

多线程实战双色球

作者:互联网

随机数索引生成代码:

 

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 FormUnionLottocs : Form
    {
        public FormUnionLottocs()
        {
          
            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 bool isGo = true;
        private List<Task> taskList = null;
        private void butStart_Click(object sender, EventArgs e)
        {
            taskList=new List<Task>();
            this.butStart.Text = "正在抽奖中...";
            this.butStart.Enabled = false;
            this.butStop.Enabled = true;
            //开始根据lab控件数量循环并启动同等数量的子线程个数
            foreach (var controls in this.groupBox.Controls)
            {
                if (controls is Label)
                {
                    Label  labControls= (Label)controls;

                    taskList.Add(Task.Run(() =>
                    {
                        if (labControls.Name.Contains("Blue")) //蓝色球
                        {
                            while (isGo)
                            {
                                int blueNumberIndex =new RandomHelper().GetRandomNumberDelay(0, 16);  //最大索引是15 
                                string atPresentBlueNumer = BlueNumber[blueNumberIndex];

                                //子线程不能操作控件,所以应该通过委托交由主线程来操作控件
                                this.Invoke(new Action(() =>
                                {
                                    labControls.Text = atPresentBlueNumer;
                                }));
                            }
                        }
                        else if(labControls.Name.Contains("Red")) //红色球,可以不判断
                        {
                            while (isGo)
                            {
                                //获取随机的索引号
                                int redNumberIndex = new RandomHelper().GetRandomNumberDelay(0,33); //最大索引是32
                                string atPresentRedNumber = RedNumber[redNumberIndex];

                                lock (lockObject)
                                {
                                    var redNumberList = GetAtPresentRedLableTextValue();
                                    if (redNumberList != null && !redNumberList.Contains(atPresentRedNumber))
                                    {
                                        //子线程不能操作控件,所以应该通过委托交由主线程来操作控件
                                        this.Invoke(new Action(() =>
                                        {
                                            labControls.Text = atPresentRedNumber;

                                        }));
                                    }
                                }
                            }
                        }

                    }));
                }
            }

            TaskFactory taskFactory=new TaskFactory();
            taskFactory.ContinueWhenAll(taskList.ToArray(), ts => {

                ShowWinningNumberMessg();
                this.isGo = true;
                this.butStart.Text = "Start";
                this.butStart.Enabled = true;
                this.butStop.Enabled = false;
            });
            
        }

        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}");
        }

        /// <summary>
        /// 获取当前时间的红色label控件的值
        /// </summary>
        /// <returns></returns>
        private List<string> GetAtPresentRedLableTextValue()
        {
            List<string> strRedNumberList=new List<string>();
            foreach (var controls in this.groupBox.Controls)
            {
                //判断该控件是否是label控件 并且该控件是否是红色的label
                if (controls is Label && ((Label)controls).Name.Contains("Red"))
                {
                    ///将符合条件的label控件的text的值放入集合
                    strRedNumberList.Add(((Label)controls).Text);
                }
            }

            if (strRedNumberList.Count(c => c.Contains("00")) == 0 && strRedNumberList.Distinct().Count() < 6)
            {
                Debug.WriteLine("***********************含有重复数据***********************");
                foreach (string numberItem in strRedNumberList)
                {
                    Debug.WriteLine(numberItem);
                }
            }
            return strRedNumberList;
        }

        private void butStop_Click(object sender, EventArgs e)
        {
            this.isGo = false;
        }
    }
}

  

 

标签:实战,case,双色球,控件,Text,System,seed,using,多线程
来源: https://www.cnblogs.com/yangpengNetCore/p/16637970.html