-4

How can i measure time between click in the way that if the time between button clicks is lets say >=1000 ms (1 sec) something happends, eg. Msgbox pops out.

private void button1_Click(object sender, EventArgs e)
{
    Stopwatch sw = new Stopwatch();
    double duration = sw.ElapsedMilliseconds;
    double tt = 2000;

    sw.Start();

    if (duration >= tt)
    {
        textBox1.Text = "Speed reached!";
    }
    else
    {
        sw.Stop();
        duration = 0;
    }
}
Kevin Kopf
  • 13,327
  • 14
  • 49
  • 66
Mayketi
  • 93
  • 11

2 Answers2

1

You can do it like this:

  • On first click start the timer with time interval of 1000 ms
  • On second click stop the timer, or reset it back to zero

If the timer finishes without interruption, its event handler displays the message box.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • I don't think that your answer is useful for people looking to solve the problem. If I was a visitor wanting to solve the same problem as the OP, I would not find your answer helpful. It's just a brief description of the concept (for one option), not a real solution to the problem – musefan Sep 20 '16 at 11:25
  • 2
    @musefan My answer is useful to people wanting to solve the same problem independently. It is not useful to people looking for copy-paste code to solve their problem. – Sergey Kalinichenko Sep 20 '16 at 11:29
  • @dasblinkenlight: If people want to try and solve the problem 'independently', then they wouldn't be looking for the answer on SO in the first place. They would be trying to solve the problem themselves. In my opinion it's a poor answer, so I downvoted it. The rest of you can have whatever opinion you like... also, I think your idea is a bit backwards anyway, so even if you coded it I still wouldn't like it (though I may not have voted with the code there) – musefan Sep 20 '16 at 11:30
  • 1
    @musefan I respectfully disagree: SO is not a place where people have their problems solved for them (although some people do abuse the plase as if it were) it's a place where they come when they get stuck. The point of an answer is to get them "unstuck", which is precisely what this answer does. Thank you for letting me keep my opinion, too, although I don't need anyone's permission. Have a nice day. – Sergey Kalinichenko Sep 20 '16 at 11:37
0

As you have specifically tried to code using the Stopwatch class then I will provide a solution using that.

The problem with your attempt is that you need to declare your Stopwatch instance as a global variable, so you can access the same instance on different click events.

Stopwatch sw = new Stopwatch();

private void button1_Click(object sender, EventArgs e)
{
    // First we need to know if it's the first click or the second.
    // We can do this by checking if the timer is running (i.e. starts on first click, stops on second.
    if(sw.IsRunning) // Is running, second click.
    {
        // Stop the timer and compare the elapsed time.
        sw.Stop();
        if(sw.ElapsedMilliseconds > 1000)
        {
            textBox1.Text = "Speed reached!";
        }
    }
    else // Not running, first click.
    {
        // Start the timer from 0.
        sw.Restart();
    }
}
musefan
  • 47,875
  • 21
  • 135
  • 185