1 #region 每个思考时间完成以后发送指定的数据量,不管服务器是否处理完 2 3 for (int i = 0; i < _threadNumber; i++) 4 { 5 Task.Factory.StartNew(new Action<object>(t => 6 { 7 PrintLog($@"ThreadId:{Thread.CurrentThread.ManagedThreadId},Starting Process {i}"); 8 Do((int)t); 9 }), i); 10 } 11 12 #endregion 13 14 #region 发送N次请求以后等待所有线程完成,等服务器处理完所有的请求后,再执行下一次发送 15 //var tasks = new List<Task<Tuple<int, bool>>>(); 16 //for (int i = 0; i < _threadNumber; i++) 17 //{ 18 // PrintLog($@"ThreadId:{Thread.CurrentThread.ManagedThreadId},Starting Process {i}"); 19 // tasks.Add(DoAsync(i)); 20 //} 21 22 //foreach (var task in Task.WhenAll(tasks).GetAwaiter().GetResult()) 23 //{ 24 // if (task.Item2) 25 // { 26 // PrintLog($@"ThreadId:{Thread.CurrentThread.ManagedThreadId},Ending Process {task.Item1}:{task.Item2}"); 27 // } 28 //} 29 #endregion
1 public void Do(int i) 2 { 3 PrintLog($@"ThreadId:{Thread.CurrentThread.ManagedThreadId},working..{i}"); 4 Thread.Sleep(1000); 5 PrintLog($@"ThreadId:{Thread.CurrentThread.ManagedThreadId},worked..{i}"); 6 } 7 8 9 public async Task<Tuple<int, bool>> DoAsync(int i) 10 { 11 PrintLog($@"ThreadId:{Thread.CurrentThread.ManagedThreadId},working..{i}"); 12 await Task.Delay(3000); 13 PrintLog($@"ThreadId:{Thread.CurrentThread.ManagedThreadId},worked..{i}"); 14 return Tuple.Create(i, true); 15 }