如何以最小的时间分辨率显示指定的功能确切的起停时间 [英] How to display the specified function exact start and stop time in smallest time resolution

查看:177
本文介绍了如何以最小的时间分辨率显示指定的功能确切的起停时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图获得 interfaces.GetIPv4Statistics()。BytesReceived; 的确切开始和停止时间。

  NetworkInterface接口; 
public Form1()
{
InitializeComponent();
if(NetworkInterface.GetIsNetworkAvailable())
{
interfaces = NetworkInterface.GetAllNetworkInterfaces()[0];
}
秒表计时器= Stopwatch.StartNew();
var time1 = DateTime.Now.ToString(HH:mm:ss:fff);
//定时器停止功能需要多少时间?怎么不忽视?
timer.Stop();
TimeSpan timespan = timer.Elapsed;
//转换为10ms
Console.WriteLine(转换花费时间{0:00}:{1:00}:{2:000},
timespan.Minutes,timespan 。秒,时间秒数);

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(开始:\t {0} \\\
Bytes Received:\t {1} \\\
Stop:\t {2},
timeStartGetStatistic, currentByteReceive,timeEndGetStatisticAndConvert);

我使用秒表获取 DateTime.Now.ToString(HHmmss);



我以为 timeEndGetStatisticAndConvert 是时间也包括转换为字符串的时间。



但结果是


转换花费时间00:00:010



开始:23:04:12:134



收到的字节数:700116647



停止:23:04:12:134


开始停止时间是分辨率 1ms 相同 !!



所以秒表显示错误的时间段?



DateTime.Now.ToString()不能像想象一样?



或者当我们显示DateTime.Now.ToString()的结果时,它首先得到时间,然后只转换为字符串? (显然这是对这个逻辑错误的答案和抱歉)



顺便说一下,我验证了这个

 秒表计时器= Stopwatch.StartNew(); 
var currentByteReceive1 = interfaces.GetIPv4Statistics()。BytesReceived;
timer.Stop();

0ms ....



所以我不知道在C#中,什么是SMALLEST时间分辨率可以用来显示当前时间和如何显示?



$ interfaces.GetIPv4Statistics()。BytesReceived; 的确切开始和停止时间由此



显示矛盾发生在此!!!



真正的功能开始时间应该是 10秒以后我第一次获得 / strong>但不是BEFORE
然后我将开始时间大于结束时间!!!

  //变量名称更改,因为实际情况
//应该添加POSITIVE timespan.Milliseconds但不添加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(开始:\t {0} \\\
Bytes Received:\t {1} \\\
Stop:\t {2},
timeStartGetStatisticAndConvert, currentByteReceive,timeEndGetStatistic);




转换花费时间00:00:010



//如果更改标志,开始:23:04:14:124



开始:23:04:12: p>

收到的字节数:700116647



停止:23:04:12:134


谢谢。我会在另一篇文章中问矛盾部分。

解决方案

尝试使用Elapsed或Tick属性

 秒表计时器= Stopwatch.StartNew(); 
var currentByteReceive1 = interfaces.GetIPv4Statistics()。BytesReceived;
timer.Stop();
Console.WriteLine(timer.Elapsed);

它给我这个00:00:00.0026059(2,6 ms。)


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.

解决方案

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.)

这篇关于如何以最小的时间分辨率显示指定的功能确切的起停时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆