Gold
Gold

Reputation: 62554

how to measure time between 2 button press ? (winCE + C#)

i need to measure time between 2 button press

in Windows-CE C#

how do do it ?

thank's in advance

Upvotes: 0

Views: 2323

Answers (4)

Anthony Pegram
Anthony Pegram

Reputation: 126942

DateTime.Now may not be precise enough for your needs. Link (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;

Upvotes: 4

Wildman
Wildman

Reputation: 1

See the static System.Environment.TickCount property.

This number of milliseconds elapsed since the system started, so calling it twice and subtracting the earlier value from the later will give you the elapsed time in milliseconds.

Upvotes: 0

David Brunelle
David Brunelle

Reputation: 6450

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.

Upvotes: 2

Itay Karo
Itay Karo

Reputation: 18296

DateTime? time;

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

Upvotes: 0

Related Questions