开发者

C# console window problem with _getch()

I开发者_如何学JAVA 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


Odds are that _getch() is a blocking call.


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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜