Search code examples
c#timestampdelta

How to get Timespan in Milliseconds


I want to get the timespan in milliseconds by comparing two timestamps with DateTime.Now and the previous DateTime. I want to check if there is an event every 10 Milliseconds or later but the totalmilliseconds from DeltaT is like 188 or so. It is too high than I am expecting that is why I think there must be somethimg wrong. Or does everything look alright?

DateTime timestamp;
DateTime timestampAlt;
TimeSpan deltaT;

public void OnSensorChanged(SensorEvent e)
        {

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

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

UPDATE:

I really appreciate all of you answers but I think there is a misunderstanding (my mistake sry). So here again: Whenever the listener recognizes an event, I want to save the timestamp and compare with the timespamp of the event before. If there is a gap of more than 10 Milliseconds between the 2 events, then I do want to know more about this new event. If not, I dont even want to continue and will leave by a return.

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

Solution

  • You can use deltaT.TotalMilliseconds which expresses your delta in milliseconds. Therefore your check could be rewritten as

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

    10 is a value I inferred. It might not be what you need, but your question is partial. This answer addresses your particular question, however if you need to measure the duration of a task you should use the Stopwatch class, which is designed for that purpose.