tags:

views:

163

answers:

3

hi

i need to measure time between 2 button press

in Windows-CE C#

how do do it ?

thank's in advance

+2  A: 

Define a variable when the button is clicked once to NOW(). When you click a second time, measure the difference between NOW and your variable.

By doing NOW - a DateTime variable, you get a TimeSpan variable.

David Brunelle
A: 
DateTime? time;

buttonClick(....)
{
    if (time.HasValue)
    {
        TimeSpan diff = DateTime.Now.Subtract(time.Value);
            DoSomethingWithDiff(diff);
        time = null;
    }
    else
    {
        time = DateTime.Now;
    }
}
Itay
+4  A: 

DateTime.Now may not be precise enough for your needs. http://blogs.msdn.com/b/ericlippert/archive/2010/04/08/precision-and-accuracy-of-datetime.aspx (Short short version: DateTime is extremely precise, DateTime.Now -> not so much.)

If you want better precision, use the Stopwatch class (System.Diagnostics.Stopwatch).

Stopwatch watch = new Stopwatch();
watch.Start();

// ...

watch.Stop();
long ticks = watch.ElapsedTicks;
Anthony Pegram
Gold
There are 10 million ticks in a second. But Stopwatch has properties for EllapsedSeconds (self-explanatory) and Ellapsed, which will return a TimeSpan from which you can get minutes, seconds, etc.
Anthony Pegram
@Anthony: the default stopwatch implementation uses the system tick, which is only 1,000 ticks per seconds, not 10 million.
ctacke
@ctacke, interesting. However, the documentation I've found due to your comment indicates it varies by operating system and hardware. For example, the frequency on my work machine indicates there are 2,143,115 "timer ticks" in a second as expressed by the Stopwatch class. At any rate, thanks for the correction that Stopwatch does not use the same tick implementation as DateTime.http://msdn.microsoft.com/en-us/library/system.diagnostics.stopwatch.frequency(v=VS.100).aspx
Anthony Pegram