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]