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