简体   繁体   English

如何测量两次按下按钮之间的时间? (winCE + C#)

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

i need to measure time between 2 button press我需要测量两次按下按钮之间的时间

in Windows-CE C#在 Windows-CE C# 中

how do do it ?怎么做?

thank's in advance提前致谢

DateTime.Now may not be precise enough for your needs. DateTime.Now 可能不足以满足您的需求。 Link (Short short version: DateTime is extremely precise, DateTime.Now -> not so much.) 链接(简短的版本:DateTime 非常精确,DateTime.Now -> 不是那么多。)

If you want better precision, use the Stopwatch class (System.Diagnostics.Stopwatch).如果您想要更高的精度,请使用 Stopwatch 类 (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().将按钮单击一次时定义一个变量到 NOW()。 When you click a second time, measure the difference between NOW and your variable.当您再次单击时,测量 NOW 与您的变量之间的差异。

By doing NOW - a DateTime variable, you get a TimeSpan variable.通过执行 NOW - DateTime 变量,您将获得 TimeSpan 变量。

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.请参阅静态System.Environment.TickCount属性。

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.自系统启动以来经过的毫秒数,因此调用它两次并从后面的值中减去前面的值将为您提供以毫秒为单位的经过时间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM