473,408 Members | 2,477 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

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

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
/\/\/
Nov 21 '05 #1
13 3827
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" <mn****@wind2.nadaspam.com> wrote in message
news:62********************************@4ax.com...
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
/\/\/

Nov 21 '05 #2
In article <62********************************@4ax.com>, 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]
Nov 21 '05 #3
Hi Michael,

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

Mark
/\/\/
Nov 21 '05 #4
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
<to*@YOUKNOWTHEDRILLmtogden.com> 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.


/\/\/
Nov 21 '05 #5
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

Nov 21 '05 #6
In article <n1********************************@4ax.com>, 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]
Nov 21 '05 #7
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" <mn****@wind2.nadaspam.com> wrote in message
news:jt********************************@4ax.com...
Hi Michael,

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

Mark
/\/\/

Nov 21 '05 #8
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.


/\/\/
Nov 21 '05 #9
On 2005-03-15, Mark A Nadig <mn****@wind2.nadaspam.com> 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]
Nov 21 '05 #10
Hi Tom,

I think you may have already answered this question on another thread
from 1-28-04 http://www.mcse.ms/archive107-2004-1-337551.html

However, I'm having a heck of a time converting the c# code for the
INPUT_RECORD struct and so I can't write the DllImport stmt. Here's
the c# code you wrote a year ago :)

[StructLayout(LayoutKind.Sequential)]
public struct INPUT_RECORD
{
public InputEventType EventType;
public EVENT_UNION Event;
}

But, I can't translate to vb.net.

Anyway, thanks for all your help.

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.

/\/\/
Nov 21 '05 #11
In article <dd********************************@4ax.com>, Mark A Nadig wrote:
Hi Tom,

I think you may have already answered this question on another thread
from 1-28-04 http://www.mcse.ms/archive107-2004-1-337551.html

However, I'm having a heck of a time converting the c# code for the
INPUT_RECORD struct and so I can't write the DllImport stmt. Here's
the c# code you wrote a year ago :)

[StructLayout(LayoutKind.Sequential)]
public struct INPUT_RECORD
{
public InputEventType EventType;
public EVENT_UNION Event;
}

But, I can't translate to vb.net.

Anyway, thanks for all your help.

Mark


Mark - I'll try and translate this for you a little later on.

--
Tom Shelton [MVP]
Nov 21 '05 #12
On 2005-03-16, Mark A Nadig <mn****@wind2.nadaspam.com> wrote:
Hi Tom,

I think you may have already answered this question on another thread
from 1-28-04 http://www.mcse.ms/archive107-2004-1-337551.html

However, I'm having a heck of a time converting the c# code for the
INPUT_RECORD struct and so I can't write the DllImport stmt. Here's
the c# code you wrote a year ago :)

[StructLayout(LayoutKind.Sequential)]
public struct INPUT_RECORD
{
public InputEventType EventType;
public EVENT_UNION Event;
}

But, I can't translate to vb.net.

Anyway, thanks for all your help.

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.

/\/\/


Ok... I'm working on my Linux box again tonight - so I can't actually
test this code, but here is a translation of this structure :)

Imports System.Runtime.InteropServices

....
<Flags ()> _
Public Enum ControlKeyState
RightAltPressed = &H1
LeftAltPressed = &H2
RightCtrlPressed = &H4
LeftCtrlPressed = &H8
ShiftPressed = &H10
NumLockOn = &H20
ScrollLockOn = &H40
CapsLockOn = &H80
EnhancedKey = &H100
End Enum

<Flags ()> _
Public Enum MouseButtonState
FromLeft1stButtonPressed = &H1
RightMostButtonPressed = &H2
FromLeft2ndButtonPressed = &H4,
FromLeft3rdButtonPressed = &H8,
FromLeft4thButtonPressed = &H10,
End Enum

<Flags ()> _
Public Enum MouseEventFlags
MouseMoved = &H1
DoubleClick = &H2
MouseWheeled = &H4
End Enum

Public Enum InputEventType As Short
KeyEvent = &H1
MouseEvent = &H2
WindowBufferSizeEvent = &H4
MenuEvent = &H8
FocusEvent = &H10
End Enum

<StructLayout (LayoutKind.Sequential)> _
Public Structure COORD
Public X As Short
Public Y As Short
End Structure

<StructLayout (LayoutKind.Sequential)> _
Public Structure SMALL_RECT
Public Left As Short
Public Top As Short
Public Right As Short
Public Bottom As Short
End Structure

<StructLayout (LayoutKind.Explicit)> _
Public Structure CONSOLE_CHAR
<FieldOffset (0)> Public AsciiChar As Byte
<FieldOffset (0)> Public UnicodeChar As Short
End Structure

<StructLayout (LayoutKind.Sequential)> _
Public Structure KEY_EVENT_RECORD
Public bKeyDown As Boolean
Public wRepeatCount As Short
Public wVirtualKeyCode As Short
Public wVirtualScanCode As Short
Public uChar As CONSOLE_CHAR
Public dwControlKeyState As ControlKeyState
End Structure

<StructLayout(LayoutKind.Sequential)> _
Public Structure MOUSE_EVENT_RECORD
Public dwMousePosition As COORD
Public dwButtonState As MouseButtonState
Public dwControlKeyState As ControlKeyState
Public dwEventFlags As MouseEventFlags
End Structure

<StructLayout(LayoutKind.Sequential)> _
Public Structure WINDOW_BUFFER_SIZE_RECORD
Public dwSize As COORD
End Structure

<StructLayout(LayoutKind.Sequential)> _
Public Structure MENU_EVENT_RECORD
Public dwCommandId As Integer
End Structure

<StructLayout(LayoutKind.Sequential)> _
Public Structure FOCUS_EVENT_RECORD
Public bSetFocus As Boolean
End Structure

<StructLayout(LayoutKind.Explicit)> _
Public Structure EVENT_UNION
<FieldOffset(0)> Public KeyEvent As KEY_EVENT_RECORD
<FieldOffset(0)> Public MouseEvent As MOUSE_EVENT_RECORD
<FieldOffset(0)> Public WindowBufferSizeEvent As WINDOW_BUFFER_SIZE_RECORD
<FieldOffset(0)> Public MenuEvent As MENU_EVENT_RECORD
<FieldOffset(0)> Public FocusEvent As FOCUS_EVENT_RECORD
End Structure

<StructLayout(LayoutKind.Sequential)> _
Public Structure INPUT_RECORD
Public EventType As InputEventType
Public [Event] As EVENT_UNION
End Structure

Whoooh! Anyway, I think this should be pretty close. Let me know if
you have any problems.

--
Tom Shelton [MVP]
Nov 21 '05 #13
Thanks for all your help. You've been very helpful.

Mark
/\/\/
Nov 21 '05 #14

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Andras Kovacs | last post by:
I have a "insert into" transaction that hangs because there is a lock on the table and one of its rows. (Advanced syncron replication causes this locking problem.) Unfortunately adv replication is...
3
by: Prasad | last post by:
Hi, I have a batch file which does file manipulation, I need to call this batch file using asp.net and display the o/p of the command e.g the copy command o/p of the batch file on the browser....
3
by: Sander Smeenk | last post by:
Hello! I'm trying to set up replication between two servers and even though I did everything according to the documentation, the slave keeps failing to connect to the master like this: |...
7
by: Andy K | last post by:
Hi , I have an existing database (on a Linux Red Hat ES3) where the replication function exists but does not work because the target database has been shutdown months ago . My question is...
6
by: Mark Allison | last post by:
Hi, I have an application that I want to be to run in Console mode and GUI mode. If no params are entered, I want the GUI fired up, if params are entered, then go into console mode. I believe...
9
by: John Williams | last post by:
How do I load a HTML page (via URL) and parse the DOM in a Console Application? I've successfully done all this in a Windows Application by using the WebBrowser control, calling the Navigate...
6
by: nd02tsk | last post by:
Hello I am going to do a comparison betweem PgSQL and MySQL replication system. I hear there are some replication projects available for PgSQL. Which are still active and serious, because I...
4
by: Bob Alston | last post by:
Can anyone tell me what are the files required for Replication manager and registry entries required? I think I read that there are 7 files and 3 registry entries but no specifics. I have...
3
by: Veeru71 | last post by:
We have got 2 DB2-UDB databases (DB1 & DB2) running on separate instances( Inst1 & Inst2). DB1 has got Schema1 and DB2 has got Schema2. We would like to setup some kind of replication to...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.