By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,865 Members | 1,717 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,865 IT Pros & Developers. It's quick & easy.

Console program questions?

P: n/a
I am using C# do write a console program. Can I do the following tasks:

1. Detect if a key on keyboard is pressed? and get the typed ASC II code?

2. Catch and handle the event of the Control-C?

3. Catch and handle the event when the close button of the command
prompt window?

Thanks
Nov 16 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
nick wrote:
I am using C# do write a console program. Can I do the following tasks:

1. Detect if a key on keyboard is pressed? and get the typed ASC II code?

2. Catch and handle the event of the Control-C?

3. Catch and handle the event when the close button of the command
prompt window?

Thanks


4. How to avoid a piece of code be Control-C break or close in the
middle? That's the operation of the code is itomic?
Nov 16 '05 #2

P: n/a
Hi nick,

There is no managed solution for some of the tasks. You can use PInvoke,
though.
1. Detect if a key on keyboard is pressed? and get the typed ASC II code? You can read the console to get typed symbols
Console.Read, Console.ReadLine

2. Catch and handle the event of the Control-C? You have two choces (both use win API ):
1. call SetConsoleMode API and change some of the console parameters. In
particular you can turn off ENABLE_PROCESSED_INPUT in order to be able to
read Control+C as any other ASCII
2. install ConsoleCtrlHandle in order to handle console events as Ctrl+C,
Ctrl + Break and so on. See the my example at the end of the post

3. Catch and handle the event when the close button of the command
prompt window?


Install ConsoleCtrlHandler. AFIAK you cannot stop the console form closing
but you can clean up before closing. Even more you have to complete the
clean-up in some predefined time out 20 sec according to MSDN otherwise the
system will pop-up the process terminate dialog.

So here is the example.
Note: ControlHandler routine is executed in separate thread so you need to
do some synchronizing, which I haven't done here

class Class1
{
const int CTRL_C_EVENT = 0;
const int CTRL_BREAK_EVENT = 1;
const int CTRL_CLOSE_EVENT = 2;
const int CTRL_LOGOFF_EVENT = 5;
const int CTRL_SHUTDOWN_EVENT = 6;
private delegate bool HandlerRoutineDelegate(int ctrlType);
private static HandlerRoutineDelegate handlerRoutineRef;
[DllImport("Kernel32.dll")]
private static extern bool SetConsoleCtrlHandler(HandlerRoutineDelegate
routine, bool add);
/// <summary>
/// The main entry point for the application.
/// </summary>

private static bool HandlerRoutine(int ctrlType)
{
Console.WriteLine(ctrlType);
return true;
}
[STAThread]
static void Main(string[] args)
{
//Keeps a reference to the delegate to make GC happy
handlerRoutineRef = new HandlerRoutineDelegate(HandlerRoutine);

SetConsoleCtrlHandler(handlerRoutineRef, true);
while(true)
{

}

}
}

Nov 16 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.