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

多线程实战双色球

时间:2022-08-30 03:22:09浏览次数:63  
标签:实战 case 双色球 控件 Text System seed using 多线程

随机数索引生成代码:

 

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,多线程
From: https://www.cnblogs.com/yangpengNetCore/p/16637970.html

相关文章

  • 大数据架构和算法实现之路:电商系统的技术实战 pdf
    高清扫描版下载链接:https://pan.baidu.com/s/1ed9wiUcGoxfSih5UYls5LQ点击这里获取提取码大数据架构和算法实现之路:电商系统的技术实战介绍了一些主流技术在商业项目中的......
  • 编译与反编译技术实战 pdf
    高清扫描版下载链接:https://pan.baidu.com/s/10S1P67rU97MHrOxWsCDOnQ点击这里获取提取码编译与反编译技术实战全书共14章,第1章简要介绍了本书所基于的环境与工具;第2~6章......
  • 一次有趣的多线程调度问题
    线程1----wait()1Objecto=newObject();2waitAndNotifyMap.put(urlId,o);3System.out.println(urlId+"运行到阻塞");4synchronized(o){5o.wait(1000);6......
  • C++【多线程编程】之【线程安全】
    1.线程安全是什么?在拥有共享数据的多条线程并行执行的程序中,线程安全的代码会通过同步机制保证各个线程都可以正常且正确的执行,不会出现数据污染等意外情况。2.什么情况......
  • 《Python带我起飞》——入门、进阶、商业实战_ 入门版电子书籍分享
     《Python带我起飞》——入门、进阶、商业实战PDF高清版免费下载地址 内容简介······《Python带我起飞——入门、进阶、商业实战》针对Python3.5以......
  • JS逆向实战3——AESCBC 模式解密
    爬取某省公共资源交易中心通过抓包数据可知这个data是我们所需要的数据,但是已经通过加密隐藏起来了分析首先这是个json文件,我们可以用请求参数一个一个搜但是由于我......
  • JS逆向实战2--cookie-AcwScV2加密—某招标信息网
    cookies的获取首先拿到第一次访问原链接拿到acw_tc的值,然后放到session中去再用这个session再次访问原链接拿到js加载的加密的真实数据。用了一些反混淆。最后获取这......
  • C++【多线程编程】之【初识线程创建】
    1.线程创建函数调用pthread_create(句柄、参数、函数入口,函数入口的实参)intpthread_create(....)cppthread类:thread类的创建方法比较简便。但也有很多问题需要考虑......
  • 多线程
    原子性:一个操作或多个操作要么全部执行,且执行过程不会被任何因素打断,包括其他线程,要么全部不执行   每个线程都有自己独立的工作内存,从主内存中copy内容保存在工作......
  • java使用多种方式实现多线程及线程池的使用
    ​ 一、多线程实现了什么?为了解决负载均衡问题,充分利用CPU资源.为了提高CPU的使用率,采用多线程的方式去同时完成几件事情而不互相干扰.为了处理大量的IO操作时或处理......