Console program questions?

N

nick

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
 
N

nick

nick said:
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?
 
S

Stoitcho Goutsev \(100\) [C# MVP]

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)
{

}

}


}
 

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