Results 1 to 5 of 5

Thread: console program, simultaneous password & timer

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Oct 2008
    Posts
    20

    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?

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

    Re: console program, simultaneous password & timer

    from MSDN documentation - Use the KeyAvailable property in conjunction with the ReadKey method
    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

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Oct 2008
    Posts
    20

    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

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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:
    1. Imports System.Threading
    2.  
    3. Module Module1
    4.  
    5.     Sub Main()
    6.         Console.WriteLine("Please enter a valid password within 30 seconds.")
    7.  
    8.         Const VALID_PASSWORD As String = "Password"
    9.         Dim currentPassword As String = String.Empty
    10.  
    11.         Dim warningInterval As TimeSpan = TimeSpan.FromSeconds(5)
    12.         Dim timer As Stopwatch = Stopwatch.StartNew()
    13.  
    14.         Do
    15.             Thread.Sleep(100)
    16.  
    17.             While Console.KeyAvailable
    18.                 currentPassword &= Console.ReadKey().KeyChar
    19.             End While
    20.  
    21.             If currentPassword = VALID_PASSWORD Then
    22.                 Console.WriteLine()
    23.                 Console.WriteLine("Valid password entered.")
    24.                 Thread.Sleep(500)
    25.                 Exit Do
    26.             ElseIf timer.Elapsed > warningInterval Then
    27.                 If warningInterval.TotalSeconds = 30 Then
    28.                     Console.WriteLine()
    29.                     Console.WriteLine("Time's up.")
    30.                     Thread.Sleep(500)
    31.                     Exit Do
    32.                 Else
    33.                     Console.WriteLine()
    34.                     Console.WriteLine((30 - warningInterval.TotalSeconds) & " seconds remaining.")
    35.                     warningInterval = warningInterval.Add(TimeSpan.FromSeconds(5))
    36.                 End If
    37.             End If
    38.         Loop
    39.     End Sub
    40.  
    41. 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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Oct 2008
    Posts
    20

    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.

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