PC Review


Reply
Thread Tools Rate Thread

Console replication of old CHOICE DOS command - how to timeout? - choice.vb (0/1)

 
 
Mark A. Nadig
Guest
Posts: n/a
 
      15th Mar 2005
I've got a console application in vb.net to replicate the behavior of
the old DOS command CHOICE. I've got a timer event successfully
firing, however the main thread is stuck on the console.read(). How
can I interrupt that and have it return the default errorlevel?

Any suggestions? I appreciate your thoughts.

On another note: If someone knows how to easily read a single key from
the console instead of relying on the user to hit enter, that would be
most helpful as well. I understand whidbey has some enh. in this area.

Best,
Mark Nadig
/\/\/
 
Reply With Quote
 
 
 
 
Michael C#
Guest
Posts: n/a
 
      15th Mar 2005
Use the _getch() function in MSVCRT.DLL. Here's an example:

Public Class MSVCRT
Declare Auto Function _getch Lib "msvcrt.dll" () As Char
Public Shared Function ReadKey() As Char
Return (_getch())
End Function
End Class

Module Module1
Sub Main()
Console.WriteLine("Press a key: ")
Dim p As Char = MSVCRT._getch()
Console.WriteLine(String.Format("You pressed {0}", p))
Console.WriteLine("Press ENTER to end")
p = " "
While (p <> ControlChars.Cr)
p = MSVCRT._getch()
End While
End Sub
End Module


"Mark A. Nadig" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> I've got a console application in vb.net to replicate the behavior of
> the old DOS command CHOICE. I've got a timer event successfully
> firing, however the main thread is stuck on the console.read(). How
> can I interrupt that and have it return the default errorlevel?
>
> Any suggestions? I appreciate your thoughts.
>
> On another note: If someone knows how to easily read a single key from
> the console instead of relying on the user to hit enter, that would be
> most helpful as well. I understand whidbey has some enh. in this area.
>
> Best,
> Mark Nadig
> /\/\/



 
Reply With Quote
 
Tom Shelton
Guest
Posts: n/a
 
      15th Mar 2005
In article <(E-Mail Removed)>, Mark A Nadig wrote:
> I've got a console application in vb.net to replicate the behavior of
> the old DOS command CHOICE. I've got a timer event successfully
> firing, however the main thread is stuck on the console.read(). How
> can I interrupt that and have it return the default errorlevel?
>
> Any suggestions? I appreciate your thoughts.
>
> On another note: If someone knows how to easily read a single key from
> the console instead of relying on the user to hit enter, that would be
> most helpful as well. I understand whidbey has some enh. in this area.
>
> Best,
> Mark Nadig
> /\/\/


This is C# code - but it's a program I wrote and use from a batch file:

using System;
using System.ComponentModel;
using System.Runtime.InteropServices;

namespace GetKey
{
class KeyGrabber
{
// constant values for use with the console api
private const uint STD_INPUT_HANDLE = unchecked((uint) -10);
private const uint ENABLE_LINE_INPUT = 0x0002;
private const uint ENABLE_ECHO_INPUT = 0x0004;
private static readonly IntPtr INVALID_HANDLE_VALUE = new
IntPtr(-1);

// console functions
[DllImport("kernel32", ExactSpelling=true, SetLastError=true)]
private static extern IntPtr GetStdHandle (
uint nStdHandle);

[DllImport("kernel32", ExactSpelling=true, SetLastError=true)]
private static extern bool GetConsoleMode (
IntPtr hConsoleHandle,
out uint lpMode);

[DllImport("kernel32", ExactSpelling=true, SetLastError=true)]
private static extern bool SetConsoleMode(
IntPtr hConsoleHandle,
uint dwMode);

[STAThread]
static int Main(string[] args)
{
int ret = -1; // assume failure to simplify the code...
string prompt = (args.Length != 0 ? args[0] : string.Empty);

IntPtr stdin = GetStdHandle(STD_INPUT_HANDLE);
if (stdin != INVALID_HANDLE_VALUE)
{
uint oldMode;

if (GetConsoleMode(stdin, out oldMode))
{
uint newMode = (oldMode & (~(ENABLE_LINE_INPUT | ENABLE_ECHO_INPUT)));
if (SetConsoleMode(stdin, newMode))
{
Console.Write(prompt);
ret = Console.Read();
Console.WriteLine();
SetConsoleMode(stdin, oldMode)
}
}
}

return ret;
}
}
}

Should be fairly simple to convert to vb But the important part is
the setconsolemode api call.

Bye the way, I call the program from the bat file like:

@ECHO OFF

:MENU
CD c:\program files\slrn
CLS
ECHO ----------------------------
ECHO [1] news.microsoft.com
ECHO [2] news.uswest.net
ECHO [3] privatenews.microsoft.com
ECHO [4] betanews.microsoft.com
ECHO[*] Any other to exit
ECHO ----------------------------
GETKEY "Enter Your Selection: "

IF %ERRORLEVEL%==49 GOTO MSNEWS
IF %ERRORLEVEL%==50 GOTO USWEST
IF %ERRORLEVEL%==51 GOTO PMSNEWS
IF %ERRORLEVEL%==52 GOTO BETANEWS
GOTO END

In case you haven't guessed - I use this bat file to control my
newsreader

HTH

--
Tom Shelton [MVP]
 
Reply With Quote
 
Mark A. Nadig
Guest
Posts: n/a
 
      15th Mar 2005
Hi Michael,

Thank you for the reply. Any suggestions on how to timeout that call?
Thanks,

Mark
/\/\/
 
Reply With Quote
 
Mark A. Nadig
Guest
Posts: n/a
 
      15th Mar 2005
Hi Tom,

Thank you for the reply. Any suggestions on how to timeout the
console.read() call and return a default? Thanks,

Mark

On Tue, 15 Mar 2005 09:01:15 -0800, Tom Shelton
<(E-Mail Removed)> wrote:

>This is C# code - but it's a program I wrote and use from a batch file:
>
>using System;
>using System.ComponentModel;
>using System.Runtime.InteropServices;
>
>namespace GetKey
>{
> class KeyGrabber
> {
> // constant values for use with the console api
> private const uint STD_INPUT_HANDLE = unchecked((uint) -10);
> private const uint ENABLE_LINE_INPUT = 0x0002;
> private const uint ENABLE_ECHO_INPUT = 0x0004;
> private static readonly IntPtr INVALID_HANDLE_VALUE = new
>IntPtr(-1);
>
> // console functions
> [DllImport("kernel32", ExactSpelling=true, SetLastError=true)]
> private static extern IntPtr GetStdHandle (
> uint nStdHandle);
>
> [DllImport("kernel32", ExactSpelling=true, SetLastError=true)]
> private static extern bool GetConsoleMode (
> IntPtr hConsoleHandle,
> out uint lpMode);
>
> [DllImport("kernel32", ExactSpelling=true, SetLastError=true)]
> private static extern bool SetConsoleMode(
> IntPtr hConsoleHandle,
> uint dwMode);
>
> [STAThread]
> static int Main(string[] args)
> {
> int ret = -1; // assume failure to simplify the code...
> string prompt = (args.Length != 0 ? args[0] : string.Empty);
>
> IntPtr stdin = GetStdHandle(STD_INPUT_HANDLE);
> if (stdin != INVALID_HANDLE_VALUE)
> {
> uint oldMode;
>
> if (GetConsoleMode(stdin, out oldMode))
> {
> uint newMode = (oldMode & (~(ENABLE_LINE_INPUT | ENABLE_ECHO_INPUT)));
> if (SetConsoleMode(stdin, newMode))
> {
> Console.Write(prompt);
> ret = Console.Read();
> Console.WriteLine();
> SetConsoleMode(stdin, oldMode)
> }
> }
> }
>
> return ret;
> }
> }
>}
>
>Should be fairly simple to convert to vb But the important part is
>the setconsolemode api call.


/\/\/
 
Reply With Quote
 
Chris Dunaway
Guest
Posts: n/a
 
      15th Mar 2005
The console class in .Net does not support timeouts or single key
reading. The read method waits for the Enter key to be pressed.

I believe that this has been enhanced in .Net 2.0 in that you can now
print anywhere on the console screen and wait for single key presses.

Chris

 
Reply With Quote
 
Tom Shelton
Guest
Posts: n/a
 
      15th Mar 2005
In article <(E-Mail Removed)>, Mark A Nadig wrote:
> Hi Tom,
>
> Thank you for the reply. Any suggestions on how to timeout the
> console.read() call and return a default? Thanks,
>
> Mark
>


sure... Start a timer (use the system.timers.timer or the
system.threading.timer) just before your call to console.read... If the
event fires return a default.
--
Tom Shelton [MVP]
 
Reply With Quote
 
Michael C#
Guest
Posts: n/a
 
      15th Mar 2005
Mmmm... Not really. Sounds like you need to dig even deeper into the DLLs
and find the routine to scan the keyboard, put this function in a loop and
drop out of the loop when a key is pressed or your countdown timer reaches
zero. I've actually done things similar to this using OS and BIOS level
Interrupt calls in x86 Assembler, but it's been a long, long time... Sorry
about that


"Mark A. Nadig" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Hi Michael,
>
> Thank you for the reply. Any suggestions on how to timeout that call?
> Thanks,
>
> Mark
> /\/\/



 
Reply With Quote
 
Mark A. Nadig
Guest
Posts: n/a
 
      15th Mar 2005
Hi Tom,

The sample code I posted already has a timer. However, since it just
invokes a delegate, how can the delegate method cause the thread Sub
Main is running on to stop and return some valu? Returning a value
from the delegate method doesn't have an affect on Sub Main().

Thanks,
Mark

>sure... Start a timer (use the system.timers.timer or the
>system.threading.timer) just before your call to console.read... If the
>event fires return a default.


/\/\/
 
Reply With Quote
 
Tom Shelton
Guest
Posts: n/a
 
      16th Mar 2005
On 2005-03-15, Mark A Nadig <(E-Mail Removed)> wrote:
> Hi Tom,
>
> The sample code I posted already has a timer. However, since it just
> invokes a delegate, how can the delegate method cause the thread Sub
> Main is running on to stop and return some valu? Returning a value
> from the delegate method doesn't have an affect on Sub Main().
>
> Thanks,
> Mark


Oopps... I didn't think about it. Well, I have another idea - in the
delegate method, call the WriteConsoleInput API function to write your
default value to the console input buffer. This should trigger the read
to return just as if you pressed the key.

Unfortunately, I'm working on my Linux box tonight, so I won't be able
to put together a sample until tommorow.

--
Tom Shelton [MVP]
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
wait for user choice by pressing keyboard, select the default one if timeout jjm Microsoft Windows 2000 CMD Promt 4 28th Mar 2011 05:31 PM
limit the choice of a combo box based on the choice of another steve goodrich Microsoft Access Getting Started 3 29th May 2006 06:14 PM
Cmd Console - How add to choice of fonts Filthy McNasty Windows XP Customization 3 22nd Mar 2006 04:50 PM
Wallpaper choice, Theme choice won' stick John O'Boyle Windows XP General 0 12th Jan 2006 06:02 PM
no fax console choice in Program Menu =?Utf-8?B?bTIwMDU=?= Windows XP Print / Fax 3 26th Jan 2005 05:43 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 10:34 AM.