[英]How to measure time between clicks in C#?
Hello so I want to make a code that does this. 您好,所以我想编写一个执行此操作的代码。 I keep clicking and if the time between clicks is >= 2000ms then write something in label else keep clicking. 我一直单击,如果两次之间的时间间隔> = 2000ms,则在标签中写一些内容,否则继续单击。
Stopwatch sw = new Stopwatch();
double tt = 2000;
double duration = sw.ElapsedMilliseconds;
private void button1_Click(object sender, EventArgs e)
{
sw.Start();
if (duration >= tt)
{
label1.Text = "Speed reached!";
}
else
{
sw.Stop();
duration = 0;
}
}
Modify your code as follows: 修改您的代码,如下所示:
private void button1_Click(object sender, EventArgs e)
{
sw.Stop();
if (sw.Elapsed.Milliseconds >= tt)
{
label1.Text = "Speed reached!";
}
else
{
sw.Reset();
sw.Start();
}
}
If I understand your question correctly you want something like this: 如果我正确理解了您的问题,那么您需要以下内容:
Stopwatch sw = new Stopwatch();
double tt = 2000;
private void button1_Click(object sender, EventArgs e)
{
sw.Stop();
if (sw.ElapsedMilliseconds >= tt)
{
label1.Text = "Speed reached!";
}
sw.Reset();
sw.Start();
}
This will start a stopwatch on the first click and then on each click it will measure the time between the clicks. 这将在第一次单击时启动秒表,然后在每次单击时将测量两次单击之间的时间。
sw.ElapsedMilliseconds is a value type, not a reference type If you assign it to a variable and ElapsedMilliseconds changes your variable won't change sw.ElapsedMilliseconds是值类型,而不是引用类型如果将其分配给变量并且ElapsedMilliseconds更改,则变量不会更改
Also, put start at the end of your code This should work 另外,在代码的末尾开始
Stopwatch sw = new Stopwatch();
double tt = 2000;
private void button1_Click(object sender, EventArgs e)
{
if (sw.ElapsedMilliseconds >= tt)
{
label1.Text = "Speed reached!";
}
else
{
sw.Stop();
sw.Reset();
}
sw.Start();
}
private void button1_Click(object sender, EventArgs e)
{
Session["PrevClickTime"] = Session["PrevClickTime"] ?? DateTime.Now.AddDays(-1);
if (((DateTime)Session["PrevClickTime"]).Subtract(DateTime.Now).Milliseconds >= 2000)
{
label1.Text = "Speed reached!";
}
else
{
// do y
}
Session["PrevClickTime"] = DateTime.Now
}
I would suggest another approach where you could remove the click event handler on each click and start a timer for 2 seconds and on the tick of the timer, attach the click event handler again . 我建议另一种方法,您可以删除每次单击的click事件处理程序并启动一个计时器2秒钟,然后在计时器的滴答声中再次附加click事件处理程序 。 Here is the sample code: 这是示例代码:
System.Windows.Forms.Timer timer = new System.Windows.Forms.Timer() { Interval = 2000 }; // here time in milliseconds
private void button1_Click(object sender, EventArgs e) // event handler of your button
{
button1.Click -= button1_Click; // remove the event handler for now
label1.Text = "Speed reached!";
// remove already attached tick handler if any, otherwise the handler would be called multiple times
timer.Tick -= timer_Tick;
timer.Tick += timer_Tick;
timer.Start();
}
void timer_Tick(object sender, System.EventArgs e)
{
button1.Click += button1_Click; // attach the event handler again
timer.Stop();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.