c# 多线程 双色球

学习记录。仅供参考。

知识点:

  1. 多线程
  2. Lock

环境:

  1. Visual Studio 2017

  1     public partial class Form1 : Form
  2     {
  3         private static readonly ConcurrentDictionary<int, bool> SourceDic = new ConcurrentDictionary<int, bool>(); //存放数字的字典 标记为True 表示已占用,不能再使用我
  4 
  5         private static readonly ConcurrentDictionary<int, int> ResultDic = new ConcurrentDictionary<int, int>(); //存放UI页面上次存放的数字 通过Label的Id 保存最后的结果值
  6 
  7         private static readonly Random Random = new Random(); //随机数
  8 
  9         private CancellationTokenSource _cts = new CancellationTokenSource(); //取消信号源
 10 
 11         private static readonly object LockObj = new object();//
 12 
 13         static Form1()//静态构造
 14         {
 15             foreach (var i in Enumerable.Range(1, 33))
 16             {
 17                 SourceDic.TryAdd(i, false);
 18             }
 19 
 20             foreach (var i in Enumerable.Range(1, 7))
 21             {
 22                 ResultDic.TryAdd(i, i); //1,2,3,4,5,6,7
 23             }
 24         }
 25 
 26         public Form1()//构造函数
 27         {
 28             InitializeComponent();
 29         }
 30 
 31         //开始按钮
 32         private void btnStart_Click(object sender, EventArgs e)
 33         {
 34             if (_cts.IsCancellationRequested)
 35             {
 36                 _cts = new CancellationTokenSource(); //重新生成cts
 37             }
 38 
 39             btnStart.Enabled = false;
 40             btnEnd.Enabled = true;
 41 
 42             foreach (var i in Enumerable.Range(1, 7))
 43             {
 44                 Task.Factory.StartNew(o =>
 45                 {
 46                     var controlId = Convert.ToInt32(o);
 47                     while (!_cts.IsCancellationRequested)
 48                     {
 49                         if (controlId == 7)//==7表示是绿球
 50                         {
 51                             ResultDic.TryGetValue(7, out var value);
 52                             ResultDic.TryUpdate(7, Random.Next(1, 17), value);
 53                         }
 54                         else
 55                         {
 56                             var oldValueKey = ResultDic.GetOrAdd(controlId, controlId);
 57                             var newValueKey = GetNonDuplicate(oldValueKey); 
 58                             ResultDic.TryUpdate(controlId, newValueKey, oldValueKey);
 59                         }
 60 
 61                         UpdateLabel(controlId);
 62                         Thread.Sleep(Random.Next(1, 5));//1-5毫秒随机停顿
 63                     }
 64                 }, i);
 65             }
 66         }
 67 
 68         //结束按钮
 69         private void btnEnd_Click(object sender, EventArgs e)
 70         {
 71             btnStart.Enabled = true;
 72             btnEnd.Enabled = false;
 73 
 74             _cts.Cancel();
 75         }
 76 
 77         //获取不重复数字
 78         private static int GetNonDuplicate(int oldKey)
 79         {
 80             int key;
 81             lock (LockObj)
 82             {
 83                 do
 84                 {
 85                     key = Random.Next(1, 34);
 86                 } while (oldKey == key || SourceDic.GetOrAdd(key, true)); //true表示被占用 再次重新获取
 87 
 88                 SourceDic.TryUpdate(oldKey, false, true); //清除占用
 89                 SourceDic.TryUpdate(key, true, false); ////占用
 90             }
 91 
 92             return key;
 93         }
 94 
 95         //更新Label
 96         private void UpdateLabel(int controlId)
 97         {
 98             var action = new Action(() =>
 99             {
100                 if (this.Controls["label" + controlId] is Label label)
101                 {
102                     label.Text = ResultDic[controlId].ToString();
103                 }
104 
105                 //前6个数字有值相等 报异常
106                 if (ResultDic.Take(6).Select(x => x.Value).Distinct().Count() != 6)
107                 {
108                     throw new Exception("前6位有重复值");
109                 }
110             });
111 
112             this.Invoke(action);
113         }
114     }

效果如图:

猜你喜欢

转载自www.cnblogs.com/herenwei-wayne/p/9785502.html