|
-
Jan 8th, 2009, 03:23 PM
#1
Thread Starter
Hyperactive Member
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..."
-
Jan 8th, 2009, 04:15 PM
#2
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
-
Jan 8th, 2009, 04:36 PM
#3
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
-
Jan 8th, 2009, 04:40 PM
#4
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 -
-
Jan 8th, 2009, 05:30 PM
#5
Thread Starter
Hyperactive Member
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..."
-
Jan 8th, 2009, 05:36 PM
#6
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
-
Jan 9th, 2009, 08:12 AM
#7
Re: Countdown Timer in a Console Application
 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 -
-
Jan 9th, 2009, 08:35 AM
#8
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
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|