List<task> telnetTasks = new List<task>();
for (var i = 0; i < NumTelnetSessions; I++)
{
var monitor = new TelnetMonitor(telnetAddresses[i]);
telnetTasks.Add(monitor.Run(cancellationToken));
}
Task.WhenAll(telnetTasks.ToArray());
...
</task></task>
You can stop the monitors by cancelling the cancellationToken.
TelnetMonitor.Run
is an async Task method that monitors the specified server in a loop awaiting async Send, Receive and Delay methods and checking for cancellation in the loop.