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
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;
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.
DateTime? time;
buttonClick(....)
{
if (time.HasValue)
{
TimeSpan diff = DateTime.Now.Subtract(time.Value);
DoSomethingWithDiff(diff);
time = null;
}
else
{
time = DateTime.Now;
}
}
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.
精彩评论