Unblock Console.Readline from a separate thread

  • Thread starter Thread starter Kevin
  • Start date Start date
K

Kevin

In a newsgroup thread from Jan 8, 2003 between Barry Holsinger and the
VBDotNet Team, please review this excerpt:


"You understood my problem completely. Your sample code provides a
really
elegant way to inject CrLf into the input stream, which effectively
unblocks
the ReadLine method. Last night, I had finally got the
WriteConsoleInput
API function to work correctly from VB.NET, and now I see these few
lines of
code from you that do the whole thing without resorting to kernel32
calls.

All I can say is... I wish I'd found this newsgroup two weeks ago!

Many thanks,
barryh

Stephen Martin said:
This was an interesting puzzle. If I understand correctly you want to
unblock a ReadLine request under certain circumstances, do something, and
then probably go back to reading from the console. You couldn't unblock it
on the main thread so you're putting it on another thread and trying to
abort the thread rather than unblock the read. First bit of advice is that
if any solution to a normal occurrence in your program involves calling
Thread.Abort then you are probably making going down the wrong path. Threads
should die naturally - Abort is really only for very unusual circumstances.
As to your problem the solution is to reset the standard in to another
stream, write a CrLf to that stream to unblock and then later reacquire the
standard in. It is a bit difficult to explain so I threw together a very
quick, rough example that I am attaching. This example uses separate
threads, that are created and die as needed, for console reading but it
could easily be modified to use the main thread .

HTH
"

This is EXACTLY where I'm at and it seems that I've traveled the same
path as Mr Holsinger. To try and follow what the VBDotNET team is
saying to do I've done the following:

A console app's sub main,

Sub Main()
Threading.ThreadPool.QueueUserWorkItem(AddressOf UnblockMe,
Nothing)
Console.ReadLine()

Console.WriteLine("UNBLOCKED")

Console.ReadLine()

End Sub


End Module


Private Sub UnblockMe(ByVal state As Object)
Threading.Thread.CurrentThread.Sleep(1000)

Dim MS As New IO.MemoryStream
Dim NewStream As New IO.StreamReader(MS)
Try
Console.SetIn(NewStream)

MS.WriteByte(13)
MS.WriteByte(10)
MS.Seek(0, IO.SeekOrigin.Begin)
MS.Flush()
Catch
If Not MS Is Nothing Then
MS.Close()
End If
If Not NewStream Is Nothing Then
NewStream.Close()
End If
End Try

'Put the Standard In stream back
Dim OldStream As New IO.StreamReader(Console.OpenStandardInput)
Console.SetIn(OldStream)
End Sub


This obviously is not the code to my working program, but it outlines
the crux of what I'm trying to accomplish. I need to unblock the
console.readline from a separate thread, and the above code isn't
working. And the code excerpt wasn't included in the newsgroup thread
that I was interested in.

PLEASE HELP!

Thank you and sincerely,


Kevin
 
(e-mail address removed) (Kevin) wrote in message


OK, Nobody has responded to me. In the meantime, I got the
WriteConsoleInput API call working. Here's the code for anybody else
that needs it, minus error handling.

I would still be interested in the Code that Barry Holsinger received
in the indicated thread.


CODE TO UNBLOCK A PENDING CONSOLE.READLINE
VB.NET

Imports System
Imports System.Threading
Imports System.Runtime.InteropServices

Module Module1

Sub Main()
'This will spawn a thread to unblock the ensuing
console.readline
Threading.ThreadPool.QueueUserWorkItem(AddressOf UnblockMe,
Nothing)

Console.ReadLine()
Console.WriteLine("UNBLOCKED")
Console.ReadLine()

End Sub

Private Sub UnblockMe(ByVal state As Object)
Threading.Thread.CurrentThread.Sleep(1000)

Dim InputRecords(0) As KeyEventStruct
InputRecords(0) = New KeyEventStruct

With InputRecords(0)
.EventType = 1
.bKeyDown = True
.uChar.AsciiChar = 13
.dwControlKeyState = 0
.wRepeatCount = 1
.wVirtualKeyCode = 0
.wVirtualScanCode = 0
End With

ConsoleUtils.WriteConsoleInput(ConsoleUtils.STD_INPUT_HANDLE,
InputRecords, 1, New Integer)

End Sub

End Module


Module ConsoleUtils

<Flags()> Public Enum ControlKeyState As Integer

RightAltPressed = &H1
LeftAltPressed = &H2
RightCtrlPressed = &H4
LeftCtrlPressed = &H8
ShiftPressed = &H10
NumLockOn = &H20
ScrollLockOn = &H40
CapsLockOn = &H80
EnhancedKey = &H100

End Enum

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

<DllImport("kernel32", EntryPoint:="WriteConsoleInputA",
CharSet:=CharSet.Auto, SetLastError:=True,
ThrowOnUnmappablechar:=True)> _
Public Function WriteConsoleInput( _
ByVal hConsoleInput As IntPtr, _
ByVal lpBuffer() As KeyEventStruct, _
ByVal nLength As Integer, _
ByVal lpNumberOfEventsWritten As Integer) As Boolean
End Function

<DllImport("KERNEL32.DLL", EntryPoint:="GetStdHandle",
SetLastError:=False, ExactSpelling:=True,
CallingConvention:=CallingConvention.StdCall)> _
Public Function GetStdHandle( _
ByVal nStdHandle As Integer) As Integer
End Function

<StructLayout(LayoutKind.Sequential)> Public Structure
KeyEventStruct
Public EventType As Short
<MarshalAs(UnmanagedType.Bool)> Public bKeyDown As Boolean
Public wRepeatCount As Short
Public wVirtualKeyCode As Short
Public wVirtualScanCode As Short
Public uChar As CHAR_UNION
Public dwControlKeyState As ControlKeyState
End Structure

'Public ReadOnly STD_OUTPUT_HANDLE As IntPtr = New
IntPtr(GetStdHandle(-11))
Public ReadOnly STD_INPUT_HANDLE As IntPtr = New
IntPtr(GetStdHandle(-10))
'Public ReadOnly STD_ERROR_HANDLE As IntPtr = New
IntPtr(GetStdHandle(-12))

End Module


Sincerely,

Kevin C.
 
Back
Top