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" <sm*****@removethis.emsoft.andthis.ca> wrote in message
news:#Y1BZAAuCHA.1676@TK2MSFTNGP10...
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