static void Main(string[] args) { if (args.Length != 3) { Console.WriteLine("Invalid: specify host, port, and the number of bots to run"); Console.WriteLine("Usage: EOBot.exe <host> <port> <numbots>"); return; } string host = args[0]; ushort port; if (!ushort.TryParse(args[1], out port)) { Console.WriteLine("Invalid: port number could not be parsed!"); return; } int numBots; if (!int.TryParse(args[2], out numBots)) { Console.WriteLine("Invalid: specify an integer argument"); return; } if (numBots > 25) { Console.WriteLine("Invalid: unable to launch > 25 threads of execution. Please use 25 or less."); return; } if (numBots < 1) { Console.WriteLine("Invalid: unable to launch < 1 thread of execution. Please use 1 or more."); return; } Console.WriteLine("Starting bots..."); using (Semaphore SignalDone = new Semaphore(numBots, numBots)) { BotList = new List<EOBot>(numBots); for (int i = 0; i < numBots; ++i) { SignalDone.WaitOne(); EOBot bot; try { bot = new EOBot(i, host, port); } catch (ArgumentException) { Console.WriteLine("Unable to connect to server! Host={0} Port={1}", host, port); return; } catch (TimeoutException) { Console.WriteLine("Failed initialization handshake with server! Bot num={0}", i + 1); return; } catch (InvalidOperationException) { Console.WriteLine("Invalid response from server or connection failed! Must receive an OK reply."); return; } // ReSharper disable once AccessToDisposedClosure bot.OnWorkCompleted += () => SignalDone.Release(); //this will not be disposed, as it is waited on below bot.Run(); BotList.Add(bot); Console.WriteLine("Bot {0} created. Sleeping 1100ms.", i + 1); Thread.Sleep(1100); //minimum for this is 1sec server-side } Console.WriteLine("All bots created. Waiting for termination (press CTRL+C to end early)"); Win32.SetConsoleCtrlHandler(type => { foreach (EOBot bot in BotList) { bot.Terminate(); } return true; }, true); //wait for each of the bots to complete work for (int i = 0; i < numBots; ++i) { SignalDone.WaitOne(); } } Console.WriteLine("All threads completed."); foreach (EOBot bot in BotList) { bot.Dispose(); } }
static void Main(string[] args) { if (args.Length != 3) { Console.WriteLine("Invalid: specify host, port, and the number of bots to run"); Console.WriteLine("Usage: EOBot.exe <host> <port> <numbots>"); return; } string host = args[0]; ushort port; if (!ushort.TryParse(args[1], out port)) { Console.WriteLine("Invalid: port number could not be parsed!"); return; } int numBots; if (!int.TryParse(args[2], out numBots)) { Console.WriteLine("Invalid: specify an integer argument"); return; } if (numBots > 25) { Console.WriteLine("Invalid: unable to launch > 25 threads of execution. Please use 25 or less."); return; } if (numBots < 1) { Console.WriteLine("Invalid: unable to launch < 1 thread of execution. Please use 1 or more."); return; } Console.WriteLine("Starting bots..."); using (Semaphore SignalDone = new Semaphore(numBots, numBots)) { BotList = new List <EOBot>(numBots); for (int i = 0; i < numBots; ++i) { SignalDone.WaitOne(); EOBot bot; try { bot = new EOBot(i, host, port); } catch (ArgumentException) { Console.WriteLine("Unable to connect to server! Host={0} Port={1}", host, port); return; } catch (TimeoutException) { Console.WriteLine("Failed initialization handshake with server! Bot num={0}", i + 1); return; } catch (InvalidOperationException) { Console.WriteLine("Invalid response from server or connection failed! Must receive an OK reply."); return; } // ReSharper disable once AccessToDisposedClosure bot.OnWorkCompleted += () => SignalDone.Release(); //this will not be disposed, as it is waited on below bot.Run(); BotList.Add(bot); Console.WriteLine("Bot {0} created. Sleeping 1100ms.", i + 1); Thread.Sleep(1100); //minimum for this is 1sec server-side } Console.WriteLine("All bots created. Waiting for termination (press CTRL+C to end early)"); Win32.SetConsoleCtrlHandler(type => { foreach (EOBot bot in BotList) { bot.Terminate(); } return(true); }, true); //wait for each of the bots to complete work for (int i = 0; i < numBots; ++i) { SignalDone.WaitOne(); } } Console.WriteLine("All threads completed."); foreach (EOBot bot in BotList) { bot.Dispose(); } }