Console "Pause"

G

Guest

I am writing a console application and I would like to put a "Press any key..." message once the program is done executing. Currently, when it executes it returns to the prompt. I have tried console.read and console.readline but they both require an <Enter> key to be hit. Any suggestions

Thanks
-KC
 
P

Peter Koen

I am writing a console application and I would like to put a "Press
any key..." message once the program is done executing. Currently,
when it executes it returns to the prompt. I have tried console.read
and console.readline but they both require an <Enter> key to be hit.
Any suggestions?

Thanks,
-KC

Hi,

Console.WriteLine("Press any key...");
Console.Read();

greets
Peter

--
------ooo---OOO---ooo------

Peter Koen - www.kema.at
MCAD CAI/RS CASE/RS IAT

------ooo---OOO---ooo------
 
N

news.microsoft.com

I tried this but it still requires the <Enter> key. Do you have any other
suggestions?

KC
 
T

Tom Shelton

I am writing a console application and I would like to put a "Press any key..." message once the program is done executing. Currently, when it executes it returns to the prompt. I have tried console.read and console.readline but they both require an <Enter> key to be hit. Any suggestions?

Thanks,
-KC

You have to use P/Invoke for this to call SetConsoleMode to turn off
ENABLE_LINE_INPUT and ENABLE_ECHO_INPUT. Here is a little program that
I use to grab key values for a batch file menu... It is pretty quick
and dirty, but it does the job :)

using System;
using System.ComponentModel;
using System.Runtime.InteropServices;

namespace GetKey
{
/// <summary>
/// Summary description for Class1.
/// </summary>
class KeyGrabber
{
// constant values for use with the console api
private const uint STD_INPUT_HANDLE = unchecked((uint) -10);
private const uint ENABLE_LINE_INPUT = 0x0002;
private const uint ENABLE_ECHO_INPUT = 0x0004;
private static readonly IntPtr INVALID_HANDLE_VALUE = new
IntPtr(-1);

// console functions
[DllImport("kernel32", ExactSpelling=true, SetLastError=true)]
private static extern IntPtr GetStdHandle (
uint nStdHandle);

[DllImport("kernel32", ExactSpelling=true, SetLastError=true)]
private static extern bool GetConsoleMode (
IntPtr hConsoleHandle,
out uint lpMode);

[DllImport("kernel32", ExactSpelling=true, SetLastError=true)]
private static extern bool SetConsoleMode(
IntPtr hConsoleHandle,
uint dwMode);

/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static int Main(string[] args)
{
int ret = 0;
string prompt = (args.Length != 0 ? args[0] : string.Empty);

IntPtr stdin = GetStdHandle(STD_INPUT_HANDLE);
if (stdin != INVALID_HANDLE_VALUE)
{
uint oldMode;

if (GetConsoleMode(stdin, out oldMode))
{
uint newMode = (oldMode & (~(ENABLE_LINE_INPUT |
ENABLE_ECHO_INPUT)));
if (SetConsoleMode(stdin, newMode))
{
Console.Write(prompt);
ret = Console.Read();
Console.WriteLine();
if (!SetConsoleMode(stdin, oldMode))
{
ret = -1;
}
}
else
{
ret = -1;
}
}
else
{
ret = -1;
}
}
else
{
ret = -1;
}

return ret;
}
}
}

HTH
 
N

news.microsoft.com

Thanks! That worked great!

KC
key..." message once the program is done executing. Currently, when it
executes it returns to the prompt. I have tried console.read and
console.readline but they both require an said:
Thanks,
-KC

You have to use P/Invoke for this to call SetConsoleMode to turn off
ENABLE_LINE_INPUT and ENABLE_ECHO_INPUT. Here is a little program that
I use to grab key values for a batch file menu... It is pretty quick
and dirty, but it does the job :)

using System;
using System.ComponentModel;
using System.Runtime.InteropServices;

namespace GetKey
{
/// <summary>
/// Summary description for Class1.
/// </summary>
class KeyGrabber
{
// constant values for use with the console api
private const uint STD_INPUT_HANDLE = unchecked((uint) -10);
private const uint ENABLE_LINE_INPUT = 0x0002;
private const uint ENABLE_ECHO_INPUT = 0x0004;
private static readonly IntPtr INVALID_HANDLE_VALUE = new
IntPtr(-1);

// console functions
[DllImport("kernel32", ExactSpelling=true, SetLastError=true)]
private static extern IntPtr GetStdHandle (
uint nStdHandle);

[DllImport("kernel32", ExactSpelling=true, SetLastError=true)]
private static extern bool GetConsoleMode (
IntPtr hConsoleHandle,
out uint lpMode);

[DllImport("kernel32", ExactSpelling=true, SetLastError=true)]
private static extern bool SetConsoleMode(
IntPtr hConsoleHandle,
uint dwMode);

/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static int Main(string[] args)
{
int ret = 0;
string prompt = (args.Length != 0 ? args[0] : string.Empty);

IntPtr stdin = GetStdHandle(STD_INPUT_HANDLE);
if (stdin != INVALID_HANDLE_VALUE)
{
uint oldMode;

if (GetConsoleMode(stdin, out oldMode))
{
uint newMode = (oldMode & (~(ENABLE_LINE_INPUT |
ENABLE_ECHO_INPUT)));
if (SetConsoleMode(stdin, newMode))
{
Console.Write(prompt);
ret = Console.Read();
Console.WriteLine();
if (!SetConsoleMode(stdin, oldMode))
{
ret = -1;
}
}
else
{
ret = -1;
}
}
else
{
ret = -1;
}
}
else
{
ret = -1;
}

return ret;
}
}
}

HTH
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top