static void Main(string[] args) { var watch = new Stopwatch(); watch.Start(); System.Collections.Concurrent.ConcurrentBag <Tuple <long, decimal> > lista = new System.Collections.Concurrent.ConcurrentBag <Tuple <long, decimal> >(); ServicePointManager.DefaultConnectionLimit = 10000; //Mejora: Parametros parametrizables en variables. Enumerable.Range(0, totalLength / (int)blockSize).ParallelForEachAsync( async(x) => { var start = x * blockSize; lista.Add(await Hilo(start, x)); }, 16).Wait(); watch.Stop(); var total1 = lista.Sum(x => x.Item1); var total2 = lista.Sum(x => x.Item2); Console.WriteLine($"Total lineas: {total1}"); Console.WriteLine($"Media: {total2 / total1}"); Console.WriteLine($"Han pasado {watch.Elapsed.Minutes}:{watch.Elapsed.Seconds} segundos"); Console.ReadKey(); }
public void LockTest() { var container = new IocContainer(); var s = new SimpleCommandModelService(container); System.Collections.Concurrent.ConcurrentBag <int> bag = new System.Collections.Concurrent.ConcurrentBag <int>(); List <Task> tasks = new List <Task>(); for (int i = 0; i < 10; i++) { tasks.Add(Task.Factory.StartNew(() => { bag.Add(s.LockTest()); })); } Task.WaitAll(tasks.ToArray()); Assert.Equal(55, bag.Sum()); }