Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Pause console application 1

Status
Not open for further replies.

ComputersUnlimited

IS-IT--Management
Dec 18, 2013
37
US
I need to pause a console application for x seconds, but I would also like a or press any key to continue option.

I know about Threading.Thread.Sleep() which will pause the application for x milliseconds and console.readkey() which waits for user input, however can the two be combined in one command?

I would also like to add a countdown timer for the sleep portion.

Is this possible if so how?
 
Sure, something like:

Code:
[blue]    Public Sub WaitForKeyInConsole(Optional MaxMilliseconds As Long = 5000)
        Dim stpw As Stopwatch = Stopwatch.StartNew
        Do
            Threading.Thread.Sleep(100) [COLOR=green]' Ensure we don't block [/color]
        Loop Until Console.KeyAvailable Or stpw.ElapsedMilliseconds >= MaxMilliseconds
    End Sub[/blue]
 
Yes that is what I want. Is it possible to display the seconds remaining before the window closes on its own?

What do you mean by Ensure we don't block? Do you mean using With blocks -- for example?
 
Code:
[blue]    Public Sub WaitForKeyInConsole(Optional MaxMilliseconds As Long = 5000)
        Dim stpw As Stopwatch = Stopwatch.StartNew
        Console.Write("Time left: ")
        Dim x As Long = Console.CursorLeft
        Dim y As Long = Console.CursorTop
        Console.CursorVisible = False

        Do
            Threading.Thread.Sleep(100) [COLOR=green]' Non-blocking; try watching your CPU usage with this line commented out[/color]
            Console.SetCursorPosition(x, y)
            Console.Write((MaxMilliseconds - stpw.ElapsedMilliseconds) / 1000)
        Loop Until Console.KeyAvailable Or stpw.ElapsedMilliseconds >= MaxMilliseconds
        Console.CursorVisible = True
    End Sub[/blue]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top