【问题标题】:How to get Timespan in Milliseconds如何以毫秒为单位获得时间跨度
【发布时间】:2019-08-29 17:37:39
【问题描述】:

我想通过将两个时间戳与 DateTime.Now 和前一个 DateTime 进行比较来获得以毫秒为单位的时间跨度。我想检查每 10 毫秒或更晚是否有一个事件,但来自 DeltaT 的总毫秒数大约是 188 秒。它比我预期的要高,这就是为什么我认为一定有什么不对的原因。还是一切看起来都还好?

DateTime timestamp;
DateTime timestampAlt;
TimeSpan deltaT;

public void OnSensorChanged(SensorEvent e)
        {

            timestamp = System.DateTime.Now;    
            deltaT = timestamp - timestampAlt; 

            if (deltaT.TotalSeconds <= 0.01)
            {
                return;
            }

更新:

我非常感谢你们所有人的回答,但我认为存在误解(我的错误很抱歉)。所以又来了: 每当侦听器识别出事件时,我都想保存时间戳并与之前事件的时间戳进行比较。如果两个事件之间的间隔超过 10 毫秒,那么我确实想了解更多关于这个新事件的信息。如果没有,我什至不想继续,并且会返回。

public void OnSensorChanged(SensorEvent e)
        {

            timestamp = System.DateTime.Now;
            deltaT = timestamp - timestampAlt;

            //int deltaT2 = timestamp.Millisecond - timestampAlt.Millisecond;

            String timestampStr = timestamp.ToString("ff");
            String timestampStrA = timestampAlt.ToString("ff");


            if (deltaT.TotalMilliseconds <= 10 || deltaT.TotalMilliseconds <= -10) //deltaT.Seconds <= 0.01
            {
                return;
            }

            timestampAlt = timestamp;

            newValue = e.Values[2];
            //if (buffer[99] != 0.00) 

                                                 // if last element of list is empty, add elements to buffer
            if (buffer.Count <=99)
            {
                buffer.Add(newValue);
                zeitbuffer.Add(timestamp);

            }
            else
            {
                Ableitung(DeltaBuffer(), DeltaTime()); // if last index of list is filled, do that function
            }

            if (e.Values[2] >= 11)
            {
                try
                {
                    lock (_syncLock)
                    {
                        String z2 = newValue.ToString("0.0");
                        //noteInt2 = Convert.ToInt32(newValue); 


                        try
                        {

                            _sensorTextView2.Text = string.Format("Note: {0}", z2 );

                            eventcounter.Add(z2);

【问题讨论】:

  • 好的,那么问题是什么..?
  • AFAIK TotalMilliseconds 返回毫秒数。
  • 使用总毫秒数。毫秒只会得到结果的实际毫秒数,当增量大于一秒时,这将不起作用。
  • double diffInMilliseconds = (timestamp - timestampAlt).Duration().TotalMilliseconds;

标签: c# timestamp delta


【解决方案1】:

如果您想每n-Seconds 触发一个事件,您可以使用一个计时器,在他过去时触发一个事件:

    Timer timer = new Timer();
    timer.Interval = 100;
    timer.Elapsed += YourAmasingEvent;
    timer.Start();

    private void YourAmasingEvent(object sender, ElapsedEventArgs e)
    {
        //do something here            
        (sender as Timer).Start();
    }

使用您的代码: 我猜你想等到时间过去了,在这种情况下你必须使用这样的循环:

timestamp = System.DateTime.Now;    
deltaT = timestamp - timestampAlt; 

while(true)
{            
  if (deltaT.TotalSeconds <= 0.01)
  {
     return;
  }
  timestamp = System.DateTime.Now;
  deltaT = timestamp - timestampAlt;
}

【讨论】:

  • 这也是个好主意!我以前也见过,但我也想稍后将时间戳保存在我的事件计数器中。这就是我选择这样做的原因:)
  • 您仍然可以将时间戳保存在 YourAmasingEvent 函数中,并且 Timer 变体在准确的时间间隔测量方面总是更胜一筹
【解决方案2】:

您可以使用deltaT.TotalMilliseconds,它以毫秒为单位表示您的增量。因此,您的支票可以重写为

if (deltaT.TotalMilliseconds <= 10)
{
    return;
}

10 是我推断的值。它可能不是您所需要的,但您的问题是片面的。此答案解决了您的特定问题,但是如果您需要测量任务的持续时间,您应该使用专为此目的设计的 Stopwatch 类。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-16
    • 1970-01-01
    相关资源
    最近更新 更多