How to display the specified function exact start and stop time in smallest time resolution

StackOverflow https://stackoverflow.com/questions/22176358

  •  03-06-2023
  •  | 
  •  

Question

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.

No correct solution

OTHER TIPS

As far as I know, DateTime.Ticks provides the smallest resolution of time in C#. Beyond that you'll need high resolution timers provided by the Windows API; they are normally used for media players (which require a very high precision timer).

That looks really complicated. It could be because of the format you are selecting for your DateTime ("HH:mm:ss:fff").

interfaces.GetIPv4Statistics().BytesReceived means `interfaces is already initialized and the data is already there, I believe (i.e. BytesReceived).

Basically, what you want is:

var start = DateTime.Now;
if (NetworkInterface.GetIsNetworkAvailable())
{
    interfaces = NetworkInterface.GetAllNetworkInterfaces()[0];
}
var currentByteReceive = interfaces.GetIPv4Statistics().BytesReceived;
Console.WriteLine("Timespan: {0}", DateTime.Now - start);

If, that is, I understand what you are trying to do.

Try to use Elapsed or Tick propertie

Stopwatch timer = Stopwatch.StartNew();
            var currentByteReceive1 = interfaces.GetIPv4Statistics().BytesReceived;
            timer.Stop();
            Console.WriteLine(timer.Elapsed);

It gives me this 00:00:00.0026059 (2,6 ms.)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top