423,498 Members | 1,177 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 423,498 IT Pros & Developers. It's quick & easy.

Console.Writeline hangs if user click into the console window

P: n/a
I have a console app which does it's job and frequently spits out status
messages with Console.Writeline. I noticed that, if the user
accidentally clicks into the black console window, the cursor changes to
a filled white rectangle, and the app hangs at the next Console.Write
statement.

Can that be solved somehow? It is very dangerous for the application to
stop just because of a wrong mouse click.

I know I could reimplement the app as a windows forms app and write the
status messages to a ListView - but it is more of a fundamental
question. Since if this cannot be solved, I'd never write another
console app again for production environments.

Thanks for any help
Urs
Nov 20 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Hi Urs,
Can that be solved somehow? It is very dangerous for the application to
stop just because of a wrong mouse click.


It is a generic feature of the Console window when its "QuickEdit Mode"(
"...\ConsoleApp.exe" Properties\Options\Edit Options) is enabled, in order
to disable that feature, you should uncheck the "QuickEdit Mode" option of
your app's console window at run-time by code:
...
Declare Function GetStdHandle Lib "kernel32" (ByVal nStdHandle As Integer)
As Integer
Declare Function GetConsoleMode Lib "kernel32" (ByVal hConsoleHandle As
Integer, ByRef lpMode As Integer) As Integer
Declare Function SetConsoleMode Lib "kernel32" (ByVal hConsoleHandle As
Integer, ByVal dwMode As Integer) As Integer

Public Const STD_INPUT_HANDLE = -10&
...
Dim hConsole As Integer
Dim iMode As Integer
Dim bSuccess As Integer

hConsole = GetStdHandle(STD_INPUT_HANDLE)
'bSuccess = GetConsoleMode(hConsole, iMode)
iMode = &HB7 ' Note below
bSuccess = SetConsoleMode(hConsole, iMode)
...

Note: the &HB7 is the console's mode value when disabled "QuickEdit Mode"
on my machine, if it is different with yours, please uncomment the previous
code line('bSuccess = GetConsoleMode...), and set a BreakPoint on that
line, then at the run-time when the program stops here, right click the
titlebar of the program's console window, select "Properties" and in its
"Options" tab, uncheck the "QuickEdit Mode" item, then click OK(select
"Apply properties to current window only"). Now return to the debugger,
step run the GetConsoleMode(hConsole, iMode) code and recode iMode value it
got, then stop debugging and replace that &HB7 with the new recorded iMode
value...
Wish it helps!

Best regards,

Gary Chang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
--------------------

Nov 20 '05 #2

P: n/a
Thanks it worked, with the &HB7 value. But I wonder, can this value be
differnt on a customer's machine? If yes, is there any possibliblity to
get this value at runtime?
Gary Chang wrote:
Hi Urs,

Can that be solved somehow? It is very dangerous for the application to
stop just because of a wrong mouse click.

It is a generic feature of the Console window when its "QuickEdit Mode"(
"...\ConsoleApp.exe" Properties\Options\Edit Options) is enabled, in order
to disable that feature, you should uncheck the "QuickEdit Mode" option of
your app's console window at run-time by code:
..
Declare Function GetStdHandle Lib "kernel32" (ByVal nStdHandle As Integer)
As Integer
Declare Function GetConsoleMode Lib "kernel32" (ByVal hConsoleHandle As
Integer, ByRef lpMode As Integer) As Integer
Declare Function SetConsoleMode Lib "kernel32" (ByVal hConsoleHandle As
Integer, ByVal dwMode As Integer) As Integer

Public Const STD_INPUT_HANDLE = -10&
..
Dim hConsole As Integer
Dim iMode As Integer
Dim bSuccess As Integer

hConsole = GetStdHandle(STD_INPUT_HANDLE)
'bSuccess = GetConsoleMode(hConsole, iMode)
iMode = &HB7 ' Note below
bSuccess = SetConsoleMode(hConsole, iMode)
..

Note: the &HB7 is the console's mode value when disabled "QuickEdit Mode"
on my machine, if it is different with yours, please uncomment the previous
code line('bSuccess = GetConsoleMode...), and set a BreakPoint on that
line, then at the run-time when the program stops here, right click the
titlebar of the program's console window, select "Properties" and in its
"Options" tab, uncheck the "QuickEdit Mode" item, then click OK(select
"Apply properties to current window only"). Now return to the debugger,
step run the GetConsoleMode(hConsole, iMode) code and recode iMode value it
got, then stop debugging and replace that &HB7 with the new recorded iMode
value...
Wish it helps!

Best regards,

Gary Chang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
--------------------

Nov 20 '05 #3

P: n/a
Hi Urs,

I think that value should be unique in most Windows platform.
Thanks!

Best regards,

Gary Chang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
--------------------

Nov 20 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.