I've got a Boolean value called isPressed
which is active when all keyboard and mouse buttons are active. What I want to achieve is when the isPressed
Boolean value is true
to start a timer (preferably using the Stopwatch class). While isPressed
is true keep the timer going and as soon as isPressed
is false
then stop the timer and get the difference. After it's stopped and it's true again and then stops again, add the difference to the previous timer.
I tried to use the code from this post but I couldn't get it to work. I tried doing something like:
Stopwatch stopWatch = new Stopwatch();
TimeSpan ts = stopWatch.Elapsed;
if (isPressed)
{
stopWatch.Start();
}
else
{
stopWatch.Stop();
string elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}", ts.Hours, ts.Minutes, ts.Seconds, ts.Milliseconds / 10);
lblTime.Text = string.Format("{0}", elapsedTime);
}
The lblTime
label always stayed at 0.