static async Task Main(string[] args) { var _random = new Random(); var _httpClient = new HttpClient(); var _botGenerator = new BotGenerator(_random, _httpClient); while (true) { await GenerateRandomBotsAtRandomIntervals(_random, _httpClient, _botGenerator); } }
/// <summary> /// for some randomness lets randomly simulate an influx of bot creations by creating them inparallel /// </summary> /// <param name="_random"></param> /// <param name="_httpClient"></param> /// <param name="_botGenerator"></param> /// <returns></returns> private static void InParallel(Random _random, HttpClient _httpClient, BotGenerator _botGenerator) { Parallel.For(_random.Next(0), 4, async(i) => { if (_botGenerator.GetRandomActorType() == ActorType.Robot) { await new ErrandGenerator(await _botGenerator.CreatRobot(), _random, _httpClient).DoRandomThingsRobot(); } else { await new ErrandGenerator(await _botGenerator.CreatCyborg(), _random, _httpClient).DoRandomThingsCyborg(); } }); }
private static async Task GenerateRandomBotsAtRandomIntervals(Random _random, HttpClient _httpClient, BotGenerator _botGenerator) { Thread.Sleep(_random.Next(5_000, 20_000)); if (_random.Next(3) != 1) { await Asynchronously(_random, _httpClient, _botGenerator); } else { InParallel(_random, _httpClient, _botGenerator); } }
private static async Task Asynchronously(Random _random, HttpClient _httpClient, BotGenerator _botGenerator) { var tasks = new List <Task>(); for (int i = 0; i < _random.Next(5); i++) { if (_botGenerator.GetRandomActorType() == ActorType.Robot) { tasks.Add(new ErrandGenerator(await _botGenerator.CreatRobot(), _random, _httpClient).DoRandomThingsRobot()); } else { tasks.Add(new ErrandGenerator(await _botGenerator.CreatCyborg(), _random, _httpClient).DoRandomThingsCyborg()); } } await Task.WhenAll(tasks); }