Countdown timer & Stopwatch

Stopwatch

The stopwatch is in its own tab called Stopwatch. It’s completely independent from the countdown timer and the two can work together independently. The stopwatch has the options to count time with a millisecond of precision, to count laps, to pause and to save the lap times in a text file.

The main method in here is called startMS() which is asynchronous.

public static void startMs()
{
    tempSeconds1 = (byte)DateTime.Now.Second;
    if (lapCount == 0)
    {
        lapSeconds = seconds;
        lapMinutes = minutes;
        lapHours = hours;
    }
    if (tempSeconds1 == tempSeconds2)
    {
        seconds++;
        lapSeconds++;
        tempSeconds2 = (byte)(tempSeconds1 + 1);
    }
    if (tempSeconds1 == 0 && !sixty)
    {
        seconds++;
        tempSeconds2 = 1;
        sixty = true;
    }
    if (seconds == 10) sixty = false;
    if (seconds > 59)
    {
        seconds = 0;
        minutes++;
        lapMinutes++;
    }
    if (minutes > 59)
    {
        minutes = 0;
        hours++;
        lapHours++;
    }
}

It starts after the Start button has been pressed and it’s executed once in every 33 milliseconds, which means that the refresh interval of the time is also 33ms.

Realization

For the stopwatch I’ve used the system time. It’s used to replace a high-precision timer, which would take lots of system resources. First, seconds from the system time are saved into the variable tempSeconds1. tempSeconds1 and tempSeconds2 are used to indicate the interval of a second. tempSeconds2 is declared as tempSeconds1 + 1 second. When 1 passes tempSeconds1 becomes equal to tempSeconds2 and this indicates the passing of a second. When this happens tempSeconds2 gets increased by 1 and the process begins again. To avoid the situation when tempSeconds1 is 59 and tempSeconds2 to become 60, I’ve used a boolean called “sixty”. In this way when tempSeconds2 becomes 60, it immediately changes to 0 and the minutes get increased by 1.
Seconds, minutes and hours are changed in a similar way as the countdown timer. Milliseconds however are not connected with this process. They are taken from the system time to avoid using a high-precision timer. Values are also checked for their correct representation. When the milliseconds are composed of one digit “00” is added in front of it so that a three-digit number is shown on the screen. When the milliseconds are composed of a two-digit number only “0” is added. In the same way seconds, minutes and hours are also checked. This method is executed when the boolean stopwatchRunning is true.
The “Lap” button is used for counting laps. It uses a listBox container to show the laps. Data that is saved is composed of: lap number, total time from the beginning, with precision of milliseconds and the time of the lap itself.
The “Pause” button is used for pausing. It’s function is to change the boolean stopwatchRunning to false in order to stop the time or to true to resume paused time.
”Save” is used for saving time results in a text file. Saving is possible only when the stopwatch has stopped and there is at least one lap in the list box.


Source: Github Download: Link


← Обратно