Has Microsoft ever considered adding a Pause() method to the Console class for VB/C# .NET like that in the following code?
Yes, I know the user can use the following code to create a subroutine, but it would be super-super nice if this were an actual part of the class.
Private Const m_STR_PMPT As System.String = "Please press the [Enter] key to continue . . . " 'Default user prompt.
Public Sub Pause(Optional Prompt As System.String = m_STR_PMPT, Optional ResumeInterval As System.Int64 = 0)
Try 'Attempt the following block(s) of code.
Dim r_hProc As New System.Diagnostics.Process 'An instance of a local system process.
Dim r_hInfo As New System.Diagnostics.ProcessStartInfo("cmd") 'An instance of the process' StartInfo() property.
Console.Write(Environment.NewLine & Prompt) 'Display the user prompt.
If ResumeInterval <= 0 Then 'If the user did not specify a valid timer value.
With r_hInfo 'Regarding the process start information object.
.Arguments = " /C pause >nul" 'Pass the argument to pause the process.
.UseShellExecute = False 'Indicate the process is not to be executed via the OS shell.
.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden 'Hide the process' window.
End With
Else 'If the user specified a valid timer value.
With r_hInfo 'Regarding the process start information object.
.Arguments = " /C timeout /t " & ResumeInterval & " >nul" 'Pass the argument to pause the process along with a timer value. {>nul} suppresses the console's built-in user prompt.
.UseShellExecute = False 'Indicate the process is not to be executed via the OS shell.
.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden 'Hide the process' window.
End With
End If
With r_hProc 'Regarding the process object.
.StartInfo = r_hInfo 'Get the start information for the process.
End With
r_hProc.Start() 'Start the process.
r_hProc.WaitForExit() 'Instruct the process component to wait for the associated process to exit.
r_hProc.Close() 'Free all resources associated with the process.
r_hInfo = Nothing 'Destroy the process start information object.
r_hProc.Dispose() 'Deinitialize the process object.
r_hProc = Nothing 'Destroy the process object.
Catch r_eRntm As System.Exception 'Trap any unexpected errors.
' DO ERROR HANDLING HERE
End Try
End Sub
An optional timer argument is included to allow the programmer to resume execution of the process if the user doesn't press a key. It is a very handy method for many console applications and not just for tutorials or testing. It is especially handy when you want to convey a message here and there to a user without terminating the program.