1.多線程執行方法 2.線程調用 ...
1.多線程執行方法
/// <summary> /// 隨機數保存隊列 /// </summary> private Queue<string> _randomQueue = new Queue<string>(); StringBuilder builder = new StringBuilder(); /// <summary> /// 生成隨機數,保存到隊列中 /// </summary> /// <param name="number"></param> private void BuilderRandom() { BuilderRandom(72, clickSum); } /// <summary> /// 生成隨機數,保存到隊列中 /// </summary> /// <param name="number"></param> private void BuilderRandom(int number, int clickSum) { builder.Insert(0, $"{DateTime.Now.ToString("yy-MM-dd HH:mm:ss.fff")}_第【{clickSum}】次點擊_生成【{number}】組數據_開始生成\r\n"); //builder.AppendLine($"{DateTime.Now.ToString("yy-MM-dd HH:mm:ss.fff")}_第【{clickSum}】次點擊_生成【{number}】組數據_開始生成"); Random random = new Random(Guid.NewGuid().GetHashCode()); for (int i = 0; i < number; i++) { _randomQueue.Enqueue($"第【{clickSum}】次點擊_生成【{number}】組數據_第【{number}】組數據_{DateTime.Now.ToString("yy-MM-dd HH:mm:ss.fff")}_{random.Next()}"); } builder.Insert(0, $"{DateTime.Now.ToString("yy-MM-dd HH:mm:ss.fff")}_第【{clickSum}】次點擊_生成【{number}】組數據_生成結束\r\n"); }
2.線程調用
/// <summary> /// thread生成隨機數-無參 /// </summary> /// <param name="number"></param> /// <param name="clickSum"></param> private void ThreadBuilderRandom() { Thread thread = new Thread(BuilderRandom); thread.Start(); } /// <summary> /// thread生成隨機數-有參 /// </summary> /// <param name="number"></param> /// <param name="clickSum"></param> private void ThreadBuilderRandom(int number, int clickSum) { Thread thread = new Thread(() => { BuilderRandom(number, clickSum); }); thread.Start(); }