Trying to implement a timeout parameter for connecting to a server but I'm not having much luck. Here's my code:
client = new TcpClient();
Task task = Task.Factory.FromAsync(client.BeginConnect, client.EndConnect, host, port, null);
bool taskCompleted = connectTask.Wait(timeoutInMS);
if (taskCompleted)
{
// Do something with the establishment of a successful connection
}
else
{
Console.WriteLine("Timeout!");
}
Unfortunately if timeoutInMS is greater than 1022, an AggregateException is thrown on this line:
bool taskCompleted = connectTask.Wait(timeoutInMS);
Adjusting the timeout properties of the TcpClient didn't seem to make any differences.