private void loadBlocks(List <Tuple <NodeConnection, int> > resultRemoteChainStates, int from, int to) { var tasks = new List <Task <List <Block> > >(); var requestCount = 0; while (true) { var connection = resultRemoteChainStates.Where(rc => rc.Item2 >= from).OrderBy(x => rnd.Next()).FirstOrDefault(); var newTo = from + 200; if (newTo > to) { newTo = to; } tasks.Add(connection.Item1.GetBlocks(Enumerable.Range(from, newTo).ToList())); requestCount++; from = newTo + 1; if (newTo >= to || requestCount >= _connections.Count) { break; } } Task.WaitAll(tasks.ToArray()); var blocksToSave = new List <Block>(); foreach (var taskResult in tasks.Select(t => t.Result)) { if (taskResult != null) { blocksToSave.AddRange(taskResult); } } blocksToSave = blocksToSave.OrderBy(b => b.Height).ToList(); _blockChain.AddBlocks(blocksToSave); }