473,715 Members | 5,414 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Console "Pause"

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.readlin e but they both require an <Enter> key to be hit. Any suggestions

Thanks
-KC
Nov 15 '05 #1
4 30367
"=?Utf-8?B?S2Vyd2luIEN hYnJlcmE=?=" <an*******@disc ussions.microso ft.com>
wrote in news:CD******** *************** ***********@mic rosoft.com:
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.readlin e but they both require an <Enter> key to be hit.
Any suggestions?

Thanks,
-KC


Hi,

Console.WriteLi ne("Press any key...");
Console.Read();

greets
Peter

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

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

------ooo---OOO---ooo------
Nov 15 '05 #2
I tried this but it still requires the <Enter> key. Do you have any other
suggestions?

KC
"Peter Koen" <koen-newsreply&snusn u.at> wrote in message
news:uf******** ******@TK2MSFTN GP11.phx.gbl...
"=?Utf-8?B?S2Vyd2luIEN hYnJlcmE=?=" <an*******@disc ussions.microso ft.com>
wrote in news:CD******** *************** ***********@mic rosoft.com:
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.readlin e but they both require an <Enter> key to be hit.
Any suggestions?

Thanks,
-KC


Hi,

Console.WriteLi ne("Press any key...");
Console.Read();

greets
Peter

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

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

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

Nov 15 '05 #3
In article <CD************ *************** *******@microso ft.com>, Kerwin Cabrera wrote:
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.readlin e 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_INP UT and ENABLE_ECHO_INP UT. 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.Componen tModel;
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_HANDL E = unchecked((uint ) -10);
private const uint ENABLE_LINE_INP UT = 0x0002;
private const uint ENABLE_ECHO_INP UT = 0x0004;
private static readonly IntPtr INVALID_HANDLE_ VALUE = new
IntPtr(-1);

// console functions
[DllImport("kern el32", ExactSpelling=t rue, SetLastError=tr ue)]
private static extern IntPtr GetStdHandle (
uint nStdHandle);

[DllImport("kern el32", ExactSpelling=t rue, SetLastError=tr ue)]
private static extern bool GetConsoleMode (
IntPtr hConsoleHandle,
out uint lpMode);

[DllImport("kern el32", ExactSpelling=t rue, SetLastError=tr ue)]
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(ST D_INPUT_HANDLE) ;
if (stdin != INVALID_HANDLE_ VALUE)
{
uint oldMode;

if (GetConsoleMode (stdin, out oldMode))
{
uint newMode = (oldMode & (~(ENABLE_LINE_ INPUT |
ENABLE_ECHO_INP UT)));
if (SetConsoleMode (stdin, newMode))
{
Console.Write(p rompt);
ret = Console.Read();
Console.WriteLi ne();
if (!SetConsoleMod e(stdin, oldMode))
{
ret = -1;
}
}
else
{
ret = -1;
}
}
else
{
ret = -1;
}
}
else
{
ret = -1;
}

return ret;
}
}
}

HTH
--
Tom Shelton
MVP [Visual Basic]
Nov 15 '05 #4
Thanks! That worked great!

KC
"Tom Shelton" <to*@mtogden.co m> wrote in message
news:e$******** ******@TK2MSFTN GP09.phx.gbl...
In article <CD************ *************** *******@microso ft.com>, Kerwin

Cabrera wrote:
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.readlin e 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_INP UT and ENABLE_ECHO_INP UT. 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.Componen tModel;
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_HANDL E = unchecked((uint ) -10);
private const uint ENABLE_LINE_INP UT = 0x0002;
private const uint ENABLE_ECHO_INP UT = 0x0004;
private static readonly IntPtr INVALID_HANDLE_ VALUE = new
IntPtr(-1);

// console functions
[DllImport("kern el32", ExactSpelling=t rue, SetLastError=tr ue)]
private static extern IntPtr GetStdHandle (
uint nStdHandle);

[DllImport("kern el32", ExactSpelling=t rue, SetLastError=tr ue)]
private static extern bool GetConsoleMode (
IntPtr hConsoleHandle,
out uint lpMode);

[DllImport("kern el32", ExactSpelling=t rue, SetLastError=tr ue)]
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(ST D_INPUT_HANDLE) ;
if (stdin != INVALID_HANDLE_ VALUE)
{
uint oldMode;

if (GetConsoleMode (stdin, out oldMode))
{
uint newMode = (oldMode & (~(ENABLE_LINE_ INPUT |
ENABLE_ECHO_INP UT)));
if (SetConsoleMode (stdin, newMode))
{
Console.Write(p rompt);
ret = Console.Read();
Console.WriteLi ne();
if (!SetConsoleMod e(stdin, oldMode))
{
ret = -1;
}
}
else
{
ret = -1;
}
}
else
{
ret = -1;
}
}
else
{
ret = -1;
}

return ret;
}
}
}

HTH
--
Tom Shelton
MVP [Visual Basic]

Nov 15 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
6093
by: Danny Anderson | last post by:
Hola! I am working on a program where I am including a library that came with my numerical methods textbook. The "util.h" simply includes a large number of files. I had to change the util.h slightly to adjust path names and also take into account I am working with a case-sensitive OS. My program is below. The sticky point is that adding (#include "util.h") seems to negate the (#include <string>) statement somehow. How can I get...
5
4140
by: Brian.Steele | last post by:
Greetings everyone. See http://www.spiceisle.com/cgi-bin/slideshow/slideshow.cgi?dir=brian/personal/2005/uk_trip/images&type=jpg The "Pause" button works in IE, but not in Firefox. Any ideas why? If you have any other ideas on how the javascript can be improved, please let me know.
11
39890
by: Paminu | last post by:
Is there something like system("PAUSE") for linux?
3
2758
by: RR | last post by:
I have a button on a form (form A) that opens another form. The form that opens (form B) has a listbox that is populated with a call to a function in the "on current" event. When form B with the listbox has a query listed as the data source, it works fine. In this case, the button on form A sets the record source for form B to one of several queries, depending on what is selected in a combobox on form A. When Form B first opens, an...
9
64439
by: Susan Rice | last post by:
I'm running a simple win32 console application and I want to impliment a "Press any key to continue", so I print that prompt, and then what's the easiest way to impliment reading any key? Do I use 'getchar', or 'gets', or some get function? or 'cin', or what's an easy way? What I want is the program to pause before exiting so the user can read whatever messages there are before the console window disappears.
5
6490
by: CoderMarc | last post by:
Hi, I can not get the system pause to work in a simple program. Here is my program below: // i/o example #include <iostream> using namespace std; int main ()
1
1712
by: Wazza | last post by:
G'Day, I have a re-occurring problem and wish to design a framework or pattern to address it. I have some Ideas but I would like to do some brainstorming with my peers before commencing. The problem: Several computational tasks I am running for my PhD work require several weeks to complete and I will to be able to pause these tasks
2
2334
by: Charles Zhang | last post by:
For Win32 application, I could use "__asm pause". However, for X64 platform, the inline assembler is not supported. Is there a compiler intrinsics for "__asm pause"? Thanks Charles Zhang
0
8823
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9343
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9198
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9047
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7973
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6646
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5967
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4477
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
3
2119
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.