Results 1 to 8 of 8

Thread: RE: Countdown Timer in a Console Application

Hybrid View

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2003
    Location
    Manchester, CT
    Posts
    317

    RE: Countdown Timer in a Console Application

    Hello all

    I have a console application that connects to a database. In the event the connection fails, I want it to show a countdown timer on the last line of the console window while my timer counts down to attempt another connection... how can I do that without it creating a new line for each second that ticks?

    Thanks!

    ~
    "I dont even see the code anymore... I just see Blonde, Brunette, Redhead..."

  2. #2
    Code Monkey wild_bill's Avatar
    Join Date
    Mar 2005
    Location
    Montana
    Posts
    2,993

    Re: Countdown Timer in a Console Application

    Code:
    Console.Write("10 ")
    Console.Write("9 ")
    Console.Write("8 ")
    Console.Write("7 ")
    Console.Write("6 ")
    Console.Write("5 ")
    Console.Write("4 ")
    Console.Write("3 ")
    Console.Write("2 ")
    Console.Write("1")
    Console.WriteLine()
    Would look like this:
    10 9 8 7 6 5 4 3 2 1
    That is the very essence of human beings and our very unique capability to perform complex reasoning and actually use our perception to further our understanding of things. We like to solve problems. -Kleinma

    Does your code in post #46 look like my code in #45? No, it doesn't. Therefore, wrong is how it looks. - jmcilhinney

  3. #3
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: Countdown Timer in a Console Application

    Code:
            For x As Integer = 10 To 0 Step -1
                Console.CursorLeft = 3
                Console.Write(x.ToString.PadLeft(2, " "c))
                Thread.Sleep(1000) 'for example only
            Next
            Dim s As String = Console.ReadLine
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  4. #4
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: Countdown Timer in a Console Application

    That's pretty tricky... It took me almost 30 minutes to get it working right.
    The trick is that, to remove 1 character, you ned to write a backspace + space + backspace.... Then when your value decrease from let's say 100 to 99, you need an adjustment so that it "erase" the correct amount of character on the screen... Anyway, here is the working code:
    Code:
    Module Module1
    
        Private WithEvents CountDownTimer As System.Timers.Timer
        Private countDownValue As Integer = 1200
    
        Sub Main()
            CountDownTimer = New System.Timers.Timer()
            With CountDownTimer
                .AutoReset = True
                .Interval = 100
            End With
            Console.Write("Start counting down... " & countDownValue)
            CountDownTimer.Start()
    
            'Prevent the application to close
            Console.ReadLine()
        End Sub
    
        Private Sub CountDownTimer_Elapsed(ByVal sender As Object, ByVal e As System.Timers.ElapsedEventArgs) Handles CountDownTimer.Elapsed
            Static lastDigitCount As Integer = countDownValue.ToString.Length
            countDownValue -= 1
            Dim currentDigitCount As Integer = countDownValue.ToString.Length
            'Adjustment to character erasing...
            If lastDigitCount > currentDigitCount Then
                Console.Write(ChrW(8) & ChrW(32) & ChrW(8))
                lastDigitCount = currentDigitCount
            End If
    
            'Erase the old value and write the new value
            If countDownValue > 0 Then
                For i As Integer = 0 To currentDigitCount - 1
                    Console.Write(ChrW(8) & ChrW(32) & ChrW(8))
                Next
                Console.Write(countDownValue)
            Else
                CountDownTimer.Stop()
                Console.Write(ChrW(8) & ChrW(32) & ChrW(8) & countDownValue & Environment.NewLine)
                Console.Write("Press Enter key to exit...")
            End If
        End Sub
    End Module
    Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
    - Abraham Lincoln -

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2003
    Location
    Manchester, CT
    Posts
    317

    Re: Countdown Timer in a Console Application

    Cool thanks for all of the responses... I cant wait to put this in tomorrow and see it work
    "I dont even see the code anymore... I just see Blonde, Brunette, Redhead..."

  6. #6
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: Countdown Timer in a Console Application

    Code:
            Console.Clear()
            Const countDownFrom As Integer = 100
            Dim cdfW As Integer = countDownFrom.ToString.Length
            For x As Integer = countDownFrom To 0 Step -1
                Console.CursorLeft = cdfW + 1
                Console.Write(x.ToString.PadLeft(cdfW, " "c))
                Thread.Sleep(250) 'for example only
            Next
            Dim s As String = Console.ReadLine
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  7. #7
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: Countdown Timer in a Console Application

    Quote Originally Posted by dbasnett
    Code:
            Console.Clear()
            Const countDownFrom As Integer = 100
            Dim cdfW As Integer = countDownFrom.ToString.Length
            For x As Integer = countDownFrom To 0 Step -1
                Console.CursorLeft = cdfW + 1
                Console.Write(x.ToString.PadLeft(cdfW, " "c))
                Thread.Sleep(250) 'for example only
            Next
            Dim s As String = Console.ReadLine
    @Dbasnett: did you try your code with some text already shown on the console (which ususally is the case)? Something like:
    Code:
    Dim existingText As String = "This is the text that is already displayed" & ControlChars.NewLine & "Timer counting down..."
    Console.Write(existingText)
    Const countDownFrom As Integer = 100
            Dim cdfW As Integer = countDownFrom.ToString.Length
            For x As Integer = countDownFrom To 0 Step -1
                Console.CursorLeft = cdfW + 1
                Console.Write(x.ToString.PadLeft(cdfW, " "c))
                Thread.Sleep(250) 'for example only
            Next
            Dim s As String = Console.ReadLine
    Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
    - Abraham Lincoln -

  8. #8
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: Countdown Timer in a Console Application

    Code:
            Dim cursT As Integer
            Dim cursL As Integer
            Console.WriteLine("Countdown...")
            cursT = Console.CursorTop
            cursL = Console.CursorLeft
            Const countDownFrom As Integer = 20
            Dim cdfW As Integer = countDownFrom.ToString.Length + 1
            For x As Integer = countDownFrom To 0 Step -1
                Console.SetCursorPosition(cursL, cursT)
                Console.Write(x.ToString.PadLeft(cdfW, " "c))
                Thread.Sleep(250) 'for example only
            Next
            Console.WriteLine("")
            Console.WriteLine("Press any key to continue")
            Dim s As String = Console.ReadLine
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

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