tags:

views:

38

answers:

3

Hi,

I have embedded a console window within my Windows application form using С#

I have an application that runs in the console window. my c# project is a GUI to display the output of this and also to send parameters to it.

Most is working fine except when a call to _getch() is made from my GUI to the console window it tends to freeze up the whole system.

anyone got any ideas why this would happen?

I can post code if need be.

Thank you

+2  A: 

Odds are that _getch() is a blocking call.

leppie
do you know any way I could overcome this from a windows application form?
SnoopFroggyFrog
A: 

What does _getch() do. You can start the call in another thread if its blocking the UI thread.

nitroxn
_getch() will read a keystroke from within the command prompt.Problem is, I have a program that I written for use with the command prompt. It works 100%.but when I try and use it through my GUI designed in Visual Studio by embedding the command prompt within the windows form application. it hangs, but all other functions work. so any ideas how I could handle this from my GUI?
SnoopFroggyFrog
A: 

The _getch() function won't return until the user presses a typing key on the keyboard. If this function is called from your program's main thread then this will stall the UI thread. It won't pump the message loop, your UI freezes when it no longer processes input events nor paints the windows. After a couple of seconds Windows puts the ghost window in place with "Not Responding" in the title bar.

While calling _getch() from a background thread will solve the problem, that's probably not going to be convenient. You can use _kbhit() to check if a keystroke is available. Calling _getch() after _kbhit() returns true won't block. Probably not convenient either. Trying to pump the message loop while _kbhit() returns false would technically be a solution, if it wasn't for the message loop living in the wrong code.

Note that you can type Ctrl+S in the console window to pause output, Ctrl+Q resumes it again. You'll still block the UI thread but at least you'll do it knowingly.

Hans Passant
excellent piece of info there Hans Passant, I thank you kindly for the reply :)
SnoopFroggyFrog