I tried to get the exact start and stop time of interfaces.GetIPv4Statistics().BytesReceived;
.
NetworkInterface interfaces;
public Form1()
{
InitializeComponent();
if (NetworkInterface.GetIsNetworkAvailable())
{
interfaces = NetworkInterface.GetAllNetworkInterfaces()[0];
}
Stopwatch timer = Stopwatch.StartNew();
var time1 = DateTime.Now.ToString("HH:mm:ss:fff");
//timer stop function take how much time? how if not ignored?
timer.Stop();
TimeSpan timespan = timer.Elapsed;
//10ms for the conversion
Console.WriteLine("Convert take time {0:00}:{1:00}:{2:000}",
timespan.Minutes, timespan.Seconds, timespan.Milliseconds);
var timeStartGetStatistic = DateTime.Now.ToString("HH:mm:ss:fff");
var currentByteReceive = interfaces.GetIPv4Statistics().BytesReceived;
var timeEndGetStatisticAndConvert = DateTime.Now.ToString("HH:mm:ss:fff");
Console.WriteLine("Start:\t{0}\nBytes Received:\t{1}\nStop:\t{2}",
timeStartGetStatistic, currentByteReceive, timeEndGetStatisticAndConvert);
I use Stopwatch to get the time needed for DateTime.Now.ToString("HHmmss");
I thought the timeEndGetStatisticAndConvert
is the time includes also the time for conversion to string.
but the result is
Convert take time 00:00:010
Start: 23:04:12:134
Bytes Received: 700116647
Stop: 23:04:12:134
The start and stop time is the same in resolution of 1ms!!
So Stopwatch show wrong elapsed timespan?
DateTime.Now.ToString() not function as imagine?
Or when we display the result of DateTime.Now.ToString(), it get the time first then it only convert to string? (obviously this is the answer and sorry for this logical error)
By the way, I verify this
Stopwatch timer = Stopwatch.StartNew();
var currentByteReceive1 = interfaces.GetIPv4Statistics().BytesReceived;
timer.Stop();
is 0ms....
So I wonder in C#, what is the SMALLEST time resolution that can be used to display current time and how to display it?
and finally the exact start and stop time of interfaces.GetIPv4Statistics().BytesReceived;
is showed by this
Contradiction Happen Here!!!
The real function start time should be AFTER 10ms I get the first time but not BEFORE!! And then I will have the start time larger than the end time!!!
//variable name change because the real situation
//should be Add POSITIVE timespan.Milliseconds but not Add NEGATIVE
var timeStartGetStatisticAndConvert = DateTime.Now.AddMilliseconds(-(timespan.Milliseconds)).ToString("HH:mm:ss:fff");
var currentByteReceive = interfaces.GetIPv4Statistics().BytesReceived;
var timeEndGetStatistic = DateTime.Now.ToString("HH:mm:ss:fff");
Console.WriteLine("Start:\t{0}\nBytes Received:\t{1}\nStop:\t{2}",
timeStartGetStatisticAndConvert, currentByteReceive, timeEndGetStatistic);
Convert take time 00:00:010
//If change sign, Start: 23:04:14:124
Start: 23:04:12:124
Bytes Received: 700116647
Stop: 23:04:12:134
thanks. I will ask the contradiction part in another post.