Click here to Skip to main content
15,887,175 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
I really need help on how can I fix my program.

I do stop stopwatch that will count from zero.

But the problem is that the interval is not the same as the real-time.
The seconds count from 1, then become 3 then 5 then 10 then 15, is always changing in a bigger number instead of counting 1 by 1.
It also affects the minute and hour.
I tried to change the interval of the timer, but it still did not work.

Is there any other way in which I can fix it?

What I have tried:

VB.NET
Imports System.IO
Imports System.Diagnostics

Public Class Form1
    Private stopwatch As Stopwatch
    Private elapsedTime As TimeSpan
    Private filePath As String = "stopwatch.txt"

    Private Sub StopwatchForm_Load(sender As Object, e As EventArgs) _
            Handles MyBase.Load
        stopwatch = New Stopwatch()

        If File.Exists(filePath) Then
            Dim storedTimeText As String = File.ReadAllText(filePath)
            Dim storedTimeTicks As Long

            If Long.TryParse(storedTimeText, storedTimeTicks) Then
                elapsedTime = New TimeSpan(storedTimeTicks)
            End If
        End If

        UpdateDisplay()
    End Sub

    Private Sub StartButton_Click(sender As Object, e As EventArgs) _
              Handles StartButton.Click
        If Not stopwatch.IsRunning Then
            stopwatch.Start()

            Timer1.Start()
        End If
    End Sub

    Private Sub StopButton_Click(sender As Object, e As EventArgs) _
                 Handles StopButton.Click
        If stopwatch.IsRunning Then
            stopwatch.Stop()
            Timer1.Stop()
        End If
    End Sub

    Private Sub ResetButton_Click(sender As Object, e As EventArgs) _
              Handles ResetButton.Click
        stopwatch.Reset()
        elapsedTime = TimeSpan.Zero
        UpdateDisplay()
    End Sub

    Private Sub Timer1_Tick(sender As Object, e As EventArgs) _
              Handles Timer1.Tick
        elapsedTime = elapsedTime.Add(stopwatch.Elapsed)
        UpdateDisplay()
    End Sub

    Private Sub UpdateDisplay()
        ElapsedTimeLabel.Text = elapsedTime.ToString("hh\:mm\:ss")
    End Sub

    Private Sub Form1_FormClosing(sender As Object, _
            e As FormClosingEventArgs) Handles MyBase.FormClosing
        If stopwatch.IsRunning Then
            stopwatch.Stop()
        End If

        File.WriteAllText(filePath, elapsedTime.Ticks.ToString())
    End Sub

End Class
Posted
Updated 13-Sep-23 9:30am
v2

1 solution

Come on Marlon, start thinking about your code:
VB
elapsedTime = elapsedTime.Add(stopwatch.Elapsed)
You execute that every time the timer ticks. But the time is independent of the stopwatch: the Elapsed property always returns the total time that the stopwatch has been running. Which goes up each time your timer ticks ...

So if your timer fires once per second your elapsedTime starts at zero, then the first tick it will add one. The second tick it will add two for a total of 3, then add three for a total of 6, and so on.

Dump the elapsedTime and just use the Stopwatch.Elapsed property when you want to know how long it has been running!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900