Main Site Documentation

Stopwatch


#1

Hi
Done anyone know if the microframe work has a stopwatch?

Like System.Diagnostics.Stopwatch

Thanks


#2

I don’t think it does, but you can use DateTime.Now.Ticks.

Store ticks at the beginning of the timed period, then take the delta of the ticks at the ending period and convert to your units to choice (ms, sec, min, hour, etc).


#3

Do you need to determine the elapsed time of something?


DateTime start= DateTime.Now;

// insert your code here

TimeSpan elapsed = DateTime.Now - start;




#4

Thanks guys, yes I need to collect elasped time every 100th of a second.

Does the dateTime derive from Microsoft.SPOT.Hardware.Utility.GetMachineTime() ?

Many thanks


#5

What does this mean? The elapsed time every 100th of a second is 10 milliseconds.

Are you looking to check for some condition every 10 milliseconds?


#6

Yes that right, then writed to log file.

I just couldnt find anything on stopwatch for the microframework


#7

While loop in a new thread that sleeps 10ms at the end of every loop?


#8

Yeah something like that. I just wanted to know if there was a stopwatch within the framework thats all.

Thanks all.


#9

If you need to do an action every X time units, you will want to use events/timers. It’s generally bad to loop through time in NETMF, as it is to infinite loop.


#10

Cool thanks.

I going to use a timer, every 10 millisecond get elasped time and write to log with a bit of data.

thank you


#11

You are going to have to use timers.

Using Sleep in a loop will not give you reliable timings if anything else is going on in the device. Each time a thread is given control by the framework it can hold the CPU for up to 20 milliseconds. So, if you use a thread you will have a great deal of jitter.

Using a timer and events you will have better interval accuracy, but the accuracy may not be enough for your applications.

You can get better accuracy if you use RLP © to implement the recording activity.

The Micro Framework is NOT a real-time system. You will have to do some experiments to determine if you can live with the accuracy of the measurement intervals.


#12

Thanks Mike,

Yes I will test like mad. I left my version of a stopwatch using a timer running all night last night it seams to work ok with a few anomalies as to be expected.

I was aiming for 10ms but if not. I’m not too fussed. I’ll try setting the timer to 5ms that should help. But I havent even pull data from my sensors yet!! So all is fun!

Thanks