如何测量 C# 中点击之间的时间?
How to measure time between clicks in C#?
您好,我想编写一个代码来执行此操作。我一直点击,如果点击之间的时间 >= 2000 毫秒,然后在标签中写一些东西,否则继续点击。
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;
}
}
sw.ElapsedMilliseconds是值类型,不是引用类型
如果您将它分配给一个变量并且 ElapsedMilliseconds 发生变化
你的变量不会改变
此外,将开始放在代码的末尾
这应该有效
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
}
修改你的代码如下:
private void button1_Click(object sender, EventArgs e)
{
sw.Stop();
if (sw.Elapsed.Milliseconds >= tt)
{
label1.Text = "Speed reached!";
}
else
{
sw.Reset();
sw.Start();
}
}
如果我正确理解你的问题,你想要这样的东西:
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();
}
这将在第一次点击时启动秒表,然后在每次点击时测量两次点击之间的时间。
我会建议另一种方法,您可以 删除每次点击时的点击事件处理程序 并 启动一个 2 秒的计时器 等等计时器的滴答声,再次附加点击事件处理程序。
这是示例代码:
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();
}
您好,我想编写一个代码来执行此操作。我一直点击,如果点击之间的时间 >= 2000 毫秒,然后在标签中写一些东西,否则继续点击。
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;
}
}
sw.ElapsedMilliseconds是值类型,不是引用类型 如果您将它分配给一个变量并且 ElapsedMilliseconds 发生变化 你的变量不会改变
此外,将开始放在代码的末尾 这应该有效
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
}
修改你的代码如下:
private void button1_Click(object sender, EventArgs e)
{
sw.Stop();
if (sw.Elapsed.Milliseconds >= tt)
{
label1.Text = "Speed reached!";
}
else
{
sw.Reset();
sw.Start();
}
}
如果我正确理解你的问题,你想要这样的东西:
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();
}
这将在第一次点击时启动秒表,然后在每次点击时测量两次点击之间的时间。
我会建议另一种方法,您可以 删除每次点击时的点击事件处理程序 并 启动一个 2 秒的计时器 等等计时器的滴答声,再次附加点击事件处理程序。 这是示例代码:
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();
}