Skip to content

Commit

Permalink
Improve time measurement
Browse files Browse the repository at this point in the history
  • Loading branch information
MiffyLiye committed Jun 20, 2016
1 parent 998f522 commit 2d75c6a
Showing 1 changed file with 10 additions and 7 deletions.
17 changes: 10 additions & 7 deletions TcpPing/Drivers/TcpPingDriver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

Expand Down

0 comments on commit 2d75c6a

Please sign in to comment.