多线程实现双色球

  1    public partial class frmSSQ : Form
  2     {
  3         public frmSSQ()
  4         {
  5             InitializeComponent();
  6             this.btnStart.Enabled = true;
  7             this.btnStop.Enabled = false;
  8         }
  9 
 10         #region Data
 11         /// <summary>
 12         /// 红球集合
 13         /// </summary>
 14         private string[] RedNums =
 15         {
 16             "01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12",
 17             "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24",
 18             "25", "26", "27", "28", "29", "30", "31", "32"
 19         };
 20 
 21         /// <summary>
 22         /// 蓝球集合
 23         /// </summary>
 24         private string[] BlueNums =
 25         {
 26             "01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12",
 27             "13", "14", "15", "16"
 28         };
 29 
 30         private static object _lock = new object();
 31 
 32         private bool IsGoon;
 33         #endregion
 34 
 35         #region UI
 36 
 37         private void btnStart_Click(object sender, EventArgs e)
 38         {
 39             try
 40             {
 41                 Stopwatch watch = new Stopwatch();
 42                 watch.Start();
 43 
 44                 this.btnStart.Text = "开始ing";
 45                 this.btnStart.Enabled = false;
 46                 this.IsGoon = true;
 47 
 48                 foreach (Control control in this.gbo.Controls)
 49                 {
 50                     if (control is Label)
 51                     {
 52                         ((Label)control).Text = "00";
 53                     }
 54                 }
 55                 //this.lblBlue.Text = "00";
 56                 //this.lblRed1.Text = "00";
 57                 //this.lblRed2.Text = "00";
 58                 //this.lblRed3.Text = "00";
 59                 //this.lblRed4.Text = "00";
 60                 //this.lblRed5.Text = "00";
 61                 //this.lblRed6.Text = "00";
 62                 Thread.Sleep(1000);
 63 
 64                 TaskFactory taskFactory = new TaskFactory();
 65                 List<Task> taskList = new List<Task>();
 66                 foreach (Control control in this.gbo.Controls)
 67                 {
 68                     if (control is Label)
 69                     {
 70                         Label lbl = control as Label;
 71                         taskList.Add(taskFactory.StartNew(() =>
 72                         {
 73                             while (this.IsGoon)
 74                             {
 75                                 //Thread.Sleep(100);
 76 
 77                                 string text = string.Empty;
 78 
 79                                 //1,解决冲突的最好办法,是没有冲突:
 80                                 //随机分6组,每个线程只取其中一个
 81                                 //2,线程安全的集合,出一个进一个
 82                                 //3,每次线程随意获取球,更新前检测下有没有冲突,有则重新随机,没有则更新
 83                                 if (lbl.Name.Contains("Red"))
 84                                 {
 85                                     text = this.GetRandomText();
 86                                     lock (_lock)
 87                                     {
 88                                         //text = this.DoRed(lbl);
 89                                         List<string> redList = this.GetUsedRed(lbl);
 90                                         if (redList.Contains(text))
 91                                         {
 92                                             continue;
 93                                         }
 94                                         else
 95                                         {
 96                                             this.UpdateLabel(lbl, text);
 97                                         }
 98                                     }
 99                                 }
100                                 else
101                                 {
102                                     int index = RandomHelper.GetRandomNumber(0, 16);
103                                     ////电脑是没有随机的,伪随机
104                                     //Random random = new Random();
105                                     //string text = this.BlueNums[random.Next(0, 15)];
106                                     //int index = RandomHelper.GetRandomNumber(0, 16);
107                                     text = this.BlueNums[index];
108                                     //lbl.Text = Text;//不行
109                                     this.UpdateLabel(lbl, text);
110                                 }
111                                 //this.UpdateLabel(lbl, text);
112                             }
113                         }));
114 
115                     }
116                 }
117 
118                 taskFactory.ContinueWhenAll(taskList.ToArray(),tList=>this.ShowResult());
119 
120                 this.btnStop.Enabled = true;//正确的时机打开
121             }
122             catch (Exception exception)
123             {
124                 Console.WriteLine("双色球启动出现异常:{0}", exception.Message);
125             }
126         }
127 
128         /// <summary>
129         /// 点击结束
130         /// </summary>
131         /// <param name="sender"></param>
132         /// <param name="e"></param>
133         private void btnStop_Click(object sender, EventArgs e)
134         {
135             this.btnStart.Enabled = true;
136             this.btnStop.Enabled = false;
137             this.IsGoon = false;
138             //this.ShowResult();
139         }
140         /// <summary>
141         /// 弹框提示数据
142         /// </summary>
143         private void ShowResult()
144         {
145             MessageBox.Show(string.Format("本期双色球开奖结果为:{0} {1} {2} {3} {4} {5}  篮球{6}"
146                 , this.lblRed1.Text
147                 , this.lblRed2.Text
148                 , this.lblRed3.Text
149                 , this.lblRed4.Text
150                 , this.lblRed5.Text
151                 , this.lblRed6.Text
152                 , this.lblBlue.Text));
153         }
154         #endregion
155 
156         #region private method
157 
158         private void UpdateLabel(Label label, string text)
159         {
160             this.Invoke(new Action(() => { label.Text = text; }));
161         }
162 
163         private string GetRandomText()
164         {
165             int index = RandomHelper.GetRandomNumber(0, 32);
166             return this.RedNums[index];
167         }
168 
169         //private string DoRed(Label lbl,string text)
170         //{
171         //    //int index = RandomHelper.GetRandomNumber(0, 32);
172         //    //string text = this.RedNums[index];
173 
174         //    List<string> usedList = GetUsedRed(lbl);
175         //    //foreach (Control gboControl in this.gbo.Controls)
176         //    //{
177         //    //    //当前的数字可以用
178         //    //    if (gboControl is Label && gboControl != lbl)
179         //    //    {
180         //    //        usedList.Add(((Label)gboControl).Text);
181         //    //    }
182         //    //}
183 
184         //    if (usedList.Contains(text))
185         //    {
186         //        return this.DoRed(lbl, GetRandomText());
187         //    }
188         //    else
189         //    {
190         //        return text;
191         //    }
192         //}
193 
194 
195         private List<string> GetUsedRed(Label lbl)
196         {
197             List<string> usedList = new List<string>();
198             foreach (Control gboControl in this.gbo.Controls)
199             {
200                 //当前的数字可以用
201                 if (gboControl is Label && gboControl != lbl)
202                 {
203                     usedList.Add(((Label)gboControl).Text);
204                 }
205             }
206 
207             return usedList;
208         }
209         #endregion
210     }
View Code

  帮助类:

 1     public class RandomHelper
 2     {
 3         /// <summary>
 4         /// 获取随机数
 5         /// </summary>
 6         /// <param name="min">包含,能出现的最小值</param>
 7         /// <param name="max">包含,能出现的最大值</param>
 8         /// <returns></returns>
 9         public static int GetRandomNumber(int min, int max)
10         {
11             Guid guid = Guid.NewGuid();
12             string sGuid = guid.ToString();
13             int seed = DateTime.Now.Millisecond;
14             for (int i = 0; i < sGuid.Length; i++)
15             {
16                 switch (sGuid[i])
17                 {
18                     case 'a':
19                     case 'b':
20                     case 'c':
21                     case 'd':
22                     case 'e':
23                     case 'f':
24                     case 'g':
25                         seed = seed + 1;
26                         break;
27                     case 'h':
28                     case 'i':
29                     case 'j':
30                     case 'k':
31                     case 'l':
32                     case 'm':
33                     case 'n':
34                         seed = seed + 2;
35                         break;
36                     case 'o':
37                     case 'p':
38                     case 'q':
39                     case 'r':
40                     case 's':
41                     case 't':
42                         seed = seed + 3;
43                         break;
44                     case 'u':
45                     case 'v':
46                     case 'w':
47                     case 'x':
48                     case 'y':
49                     case 'z':
50                         seed = seed + 4;
51                         break;
52                     default:
53                         seed = seed + 4;
54                         break;
55                 }
56             }
57             Random random = new Random(seed);
58             return random.Next(min, max);
59         }
60     }
View Code

  界面:

猜你喜欢

转载自www.cnblogs.com/shangec/p/10316578.html