Results 1 to 3 of 3

Thread: Countdown timer starting and ending time issue.

  1. #1

    Thread Starter
    PowerPoster Simply Me's Avatar
    Join Date
    Aug 2003
    Posts
    2,748

    Countdown timer starting and ending time issue.

    My countdown timer code
    Code:
    Private Sub CountDownTimer()
            Dim stopTime As DateTime = DateTime.Now.AddSeconds(timerStopTime)
    
            While timerRunning ' Run while the timer is running
                Dim remainingTime As TimeSpan = stopTime - DateTime.Now
    
                ' Update the timer label with the remaining time
                UpdateTimerLabel(remainingTime)
    
                If remainingTime <= TimeSpan.Zero Then
                    ' Call the common method to handle button properties and other common tasks
                    UpdateUIOnTimerStop()
                    Exit While ' Exit the loop if the remaining time is negative or zero
                End If
    
                Thread.Sleep(100) ' Adjust sleep time for smoother execution
            End While
    
            ' Ensure the timer label displays the stop time when timer stops
            UpdateTimerLabel(TimeSpan.Zero)
    
            PlayStopSound() ' Play stop sound when the timer reaches the stop time
            timerRunning = False
        End Sub
    I noticed this, that when I set the count down to say 5 seconds, and timer label shows 00:00:00.0. When I click the btnStart, the countdown starts immediately in 00:00:04.9 and starts counting down. I want it to start in 00:00:05.9, 00:00:05.8, 00:00:05.7, 00:00:05.6, 00:00:05.5, 00:00:05.4, 00:00:05.3, 00:00:05.2, 00:00:05.1, 00:00:04.0, and so on. And I also noticed that when the timer already counting at 00:00:01.9, 00:00:01.8, 00:00:01.7, 00:00:01.6, 00:00:01.5, 00:00:01.4, 00:00:01.3, 00:00:01.2, 00:00:01.1, 00:00:00.0, the timer milliseconds still countinues counting down from 9-0 (00:00:00.9, 00:00:00.8, 00:00:00.7, 00:00:00.6, 00:00:00.5, and so on) before it stops. It should stop already 00:00:00.0.
    To give is always to be NOBLE...
    To received is always to be BLESSED....
    Each day strive to be NOBLE
    Each day strive to be BLESSED

    If this post has helped you. Please take time to rate it.

    >=|+|=< Simply Me >=|+|=<

    ----------------------------------------
    Connection Strings | Number Only in Textbox | Splash Screen with Progress Bar | Printing to 1/2 of perforated bond paper |
    Freeze 2005 DataGridView Column

  2. #2
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    39,750

    Re: Countdown timer starting and ending time issue.

    I don't see what the problem is. For example, if you want to count down from 5 seconds, then the first interval will be less than 5. In fact, it will be 4.9. You'd only see the count down of 5.9, 5.8, etc, if you were counting down from 6 seconds.

    Also, why would you NOT expect 0.9, 0.8, 0.7, etc. on the way to 0.0? Those are just the values between 1 and 0 if you are counting down by 0.1 per interval. You can't very well skip over them.
    My usual boring signature: Nothing

  3. #3
    PowerPoster Poppa Mintin's Avatar
    Join Date
    Mar 2009
    Location
    Bottesford, North Lincolnshire, England.
    Posts
    2,491

    Re: Countdown timer starting and ending time issue.

    Hi,
    I guess that you set timerRunning to true with your btnStart. Setting timerRunning to false will leave the first and last two statements running. To stop that I would suggest this slight ammendment:
    Code:
     Dim stopTime As DateTime
     Dim remainingTime As TimeSpan
    
            While timerRunning ' Run while the timer is running
                stopTime = DateTime.Now.AddSeconds(timerStopTime)
                remainingTime   = stopTime - DateTime.Now
    
            '    etc
    Poppa
    Along with the sunshine there has to be a little rain sometime.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width