console program, simultaneous password & timer
I'm in a bit of a pickle here. I would like to edit one of my codes to have a password query with a timer. Basic outline of its function would be to state that the user has 30 seconds to input the correct password before the program states that the time has run out and restricts all access but does not end the program. I would like to see the timer state how many seconds is left after every 5 seconds, i.e 25.20.15.10.5.0. My problem is that I have no idea how to override console.readline command which you need for the password input.
Any ideas?
Re: console program, simultaneous password & timer
from MSDN documentation - Use the KeyAvailable property in conjunction with the ReadKey method
Re: console program, simultaneous password & timer
Hmm maybe I should've stated that I'm a total beginner at coding. I've been learning this for a few weeks, and basicly what I've got downloaded into my brain is basic if sentences, for sentences, while sentences and the most basic of statements and commands. Basicly I know the basics, but none of those fancy commands.
An example would help, one as simple as can be :)
Re: console program, simultaneous password & timer
I was intrigued by this so I thought I'd give it a bash. This would need a bit of refining to be truly usable, like giving the user the ability to reset the current password if they made a mistake, but it should give you the general idea.
vb.net Code:
Imports System.Threading
Module Module1
Sub Main()
Console.WriteLine("Please enter a valid password within 30 seconds.")
Const VALID_PASSWORD As String = "Password"
Dim currentPassword As String = String.Empty
Dim warningInterval As TimeSpan = TimeSpan.FromSeconds(5)
Dim timer As Stopwatch = Stopwatch.StartNew()
Do
Thread.Sleep(100)
While Console.KeyAvailable
currentPassword &= Console.ReadKey().KeyChar
End While
If currentPassword = VALID_PASSWORD Then
Console.WriteLine()
Console.WriteLine("Valid password entered.")
Thread.Sleep(500)
Exit Do
ElseIf timer.Elapsed > warningInterval Then
If warningInterval.TotalSeconds = 30 Then
Console.WriteLine()
Console.WriteLine("Time's up.")
Thread.Sleep(500)
Exit Do
Else
Console.WriteLine()
Console.WriteLine((30 - warningInterval.TotalSeconds) & " seconds remaining.")
warningInterval = warningInterval.Add(TimeSpan.FromSeconds(5))
End If
End If
Loop
End Sub
End Module
If you don't understand it all, PLEASE read the MSDN documentation for the types and methods I've used before posting any questions.
Re: console program, simultaneous password & timer
Thanks! That was a great help. I understand it for the most part, and I modified it to fit on to my code and it works perfectly. You really helped me out of a bad jam there. Thank you.