silmar...@o2.pl
unread,Apr 2, 2015, 9:54:52 AM4/2/15You do not have permission to delete messages in this group
Sign in to report message
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to
Hello all,
so, MS decided to kick me in the butt and drop fullscreen ability of System.Console in Windows 7. That leaves me with a task to implement a fullscreen console from scratch. Seems easy enough - fullscreen form, black backbground, no borders, maximized. The only thing to sort out is blocking keyboard input.
I have an IConsole interface which is a thin wrapper over System.Console:
public interface IConsole {
...
/// <summary>
/// Reads a character from input
/// </summary>
/// <returns>The character read from input</returns>
char ReadKey();
...
}
and the current implementation:
public class SystemConsole: IConsole {
...
public char ReadKey() {
return Console.ReadKey(true).KeyChar;
}
...
}
So basically I need to write a different implementation of IConsole using Windows.Forms. But ReadKey() is a problem. I even wrote a quick-and-dirty proof-of-concept in Java (Swing) to verify my idea:
public class GraphicalConsole implements IConsole {
...
public char ReadKey(){
semaphore.acquire();
return key;
}
public void keyPressed(KeyEvent e) {
key = e.getKeyChar();
semaphore.release();
}
...
}
Works like a charm, as Swing has a separate thread for events (apparently called "AWT-EventQueue-0").
However, the same concept doesn't work in .NET:
public partial class Form1 : Form, IConsole{
...
public char ReadKey() {
s.WaitOne();
return key;
}
private void form_KeyPress(object sender, KeyPressEventArgs e) {
key = e.KeyChar;
s.Release();
}
}
(the code is highly abstracted out for readability, but you get the idea). So the problem seems to be, the events are handled in the main thread, which is hung on the semaphore, and never gets the chance to run form_KeyPress. Classic deadlock.
So here's the question:
- is there any way I can run the event loop in a separate thread?
- is there any other way I can get a fullscreen console in .NET (like a third-party library)?
Thanks in advance.