多线程实战双色球

随机数索引生成代码:

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
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);
        }
    }
}

  具体业务实现代码:

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
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;
        }
    }
}

  

 

posted @   法外狂徒派大星  阅读(12)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 提示词工程——AI应用必不可少的技术
· 字符编码:从基础到乱码解决
· 地球OL攻略 —— 某应届生求职总结
点击右上角即可分享
微信分享提示