Redirecting console output in realtime?

M

Markus S.

Hello,

I have a problem with a DOS EXE that is called by a .Net Winforms
application. I need to redirect the console output into a textbox, but
this should happen in real time, so when new output is written to the
redirected StandardOutput-Reader, I want to catch it immediately and
add it the my textbox.

Now, I have tried everything from threads to invokes, but every time i
call the read/readline/readtoend function in the thread, it suspends at
this point. After the process is finished, my thread goes on and the
output is written to the textbox.

Does anybody have an idea how i can make the thread run the entire
time, without stopping after the first access of standardoutput?

I should add that I use VB 2003, so I am not able to use any of the new
2005 classes.

Here are some code snippets:
(the following code lies in an extra class, outside of the GUI)

'Called from the Form when "start"-button is clicked
Public Sub Start()
'See below for exact code of createProcess()
myProc = createProcess()
myProc.Start()

'New Thread for catching the StandardOutput in realtime
myThread = New Threading.Thread(AddressOf startCatch)
myThread.Name = "ConsoleCatcher"
myThread.IsBackground = True

myThread.Start()

End Sub

'Creates a Process with appropriate settings
Private Function createProcess() As Process
Dim objP As New Process
objP.StartInfo.UseShellExecute = False
objP.StartInfo.RedirectStandardOutput = True
objP.StartInfo.RedirectStandardError = True
objP.StartInfo.CreateNoWindow = True
objP.StartInfo.WorkingDirectory = Application.StartupPath

objP.StartInfo.FileName = myFile
objP.StartInfo.Arguments = myArgs

Return objP
End Function


'That's what happens in the output-catching function
Private Sub startCatch()
Try
Dim strLine As String

Do While Not myProc Is Nothing
'The Thread stops while executing the line below for the
first time.
'It continues normally after the Process belonging to the
StandardOutput has finished...
strLine = myProc.StandardOutput.ReadLine()
If strLine <> "" And strLine <> Nothing Then

'Event caught by the Form
RaiseEvent ConsoleOut(Me, New
DataReceivedEventArgs(strLine))
End If

Loop

'See if there's anything left
strLine = myReader.ReadToEnd
If strLine <> Nothing And strLine <> "" Then
RaiseEvent ConsoleOut(Me, New
DataReceivedEventArgs(strLine))
End If
Catch
End Try

End Sub


Any help is highly appreciated. Thanks alot! :)

Bye,
Markus
 
H

Herfried K. Wagner [MVP]

Markus S. said:
I have a problem with a DOS EXE that is called by a .Net Winforms
application. I need to redirect the console output into a textbox, but
this should happen in real time, so when new output is written to the
redirected StandardOutput-Reader, I want to catch it immediately and
add it the my textbox.

Now, I have tried everything from threads to invokes, but every time i
call the read/readline/readtoend function in the thread, it suspends at
this point. After the process is finished, my thread goes on and the
output is written to the textbox.

<URL:http://www.palmbytes.de/content/dotnet/console.htm>
<URL:http://dotnet.mvps.org/dotnet/samples/misc/#RedirectConsole>
 
M

Markus S.

Hi again,

the first example from
http://www.palmbytes.de/content/dotnet/console.htm is almost the same I
did. and it behaves the way my app does :)

Maybe I have to specify my problem more exactly.

I am writing a .net frontend for cabarc.exe which created cab archives.

now, let's assume I call the exe with a filelist of 20 files. when i
use the standard console output, the screen is updated after every file
(i can watch the progress in real time, as i get a line saying "file x
added to archive"). when i redirect the output and start cabarc, I
won't get a response from standardoutput.readline until the exe has
finished running...
Slowly it makes me belive that this behavior is just the way the
process class works (waiting until the underlying exe-process is
finished and then redirecting the whole bunch of lines in one step)...

or is there any workaround for this maybe?

But thanks already for the help everybody!
 
C

Cor Ligthert [MVP]

Markus,

Did you beside that palmbytes sample as well tried the sample I made for
you?

Cor
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top