diff --git a/TcpPing/Drivers/TcpPingDriver.cs b/TcpPing/Drivers/TcpPingDriver.cs index aa2b2e7..7e77566 100644 --- a/TcpPing/Drivers/TcpPingDriver.cs +++ b/TcpPing/Drivers/TcpPingDriver.cs @@ -117,17 +117,20 @@ private void SetRemoteEndPoint(string arg) { socket.Blocking = true; - var stopWatch = new Stopwatch(); - stopWatch.Start(); - // ReSharper disable once AccessToDisposedClosure - var taskConnect = Task.Run(() => socket.Connect(remote)); + var taskConnect = Task.Run(() => + { + var stopWatch = new Stopwatch(); + stopWatch.Start(); + // ReSharper disable once AccessToDisposedClosure + socket.Connect(remote); + stopWatch.Stop(); + return stopWatch.Elapsed; + }); var taskCountDown = Task.Run(() => Thread.Sleep(timeOutLimit)); Task.WaitAny(taskConnect, taskCountDown); - stopWatch.Stop(); socket.Close(); - var delay = stopWatch.Elapsed; - return taskConnect.IsCompleted ? (double?) delay.TotalMilliseconds : null; + return taskConnect.IsCompleted && !taskConnect.IsFaulted ? (double?) taskConnect.Result.TotalMilliseconds : null; } }