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!
~ :wave:
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
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
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
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 :)
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
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
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