I have this code block:
var client = new TcpClient();
HttpRequestInfo.AddTimestamp("Connecting");
await Task.WhenAny(client.ConnectAsync(serverAddress, serverPort),
Task.Delay(TimeSpan.FromMilliseconds(300)));
HttpRequestInfo.AddTimestamp("Connected");
if(client.Connected){ ... }
Where HttpRequestInfo.AddTimestamp just writes the timestamp names with the stopwatch class.
In magazines, I sometimes see:
"Connecting":110ms - "Connected":747ms
"Connecting":35ms - "Connected":3120ms
"Connecting":38ms - "Connected":3053ms
I suggested that this approach will give me the opportunity to limit the connection to a timeout (300 ms). However, I see that this line of code sometimes (very rarely) runs longer than 300 ms. What is the reason for this behavior?
source
share