Question Process.BeginOutputReadLine() causing VSHOST.EXE to fail

devilgoblin

Member
Joined
May 31, 2010
Messages
5
Location
Texas
Programming Experience
5-10
This code seems to be accurate when comparing to the many examples that are similar on different forums and I would think it should work:

Dim pC As New Process
pC.StartInfo.CreateNoWindow = True
pC.StartInfo.RedirectStandardOutput = True
pC.StartInfo.UseShellExecute = False
pC.StartInfo.FileName = Application.StartupPath & "\build.bat"
AddHandler pC.OutputDataReceived, AddressOf pC_OutputDataReceived
pC.Start()
pC.BeginOutputReadLine()
pC.WaitForExit()

Private Sub pC_OutputDataReceived(ByVal sender As Object, ByVal e As DataReceivedEventArgs)
lbOutput.Items.Add(e.Data.ToString)
End Sub

When I remove the pC.BeginOutputReadLine() command, the application continues to function, but nothing is added to lbOutput.

With the pC.BeginOutputReadLine() command, it gives the error "vshost.exe has stopped working"

Either way, my batch file does what its supposed to do. Any one have any ideas what I am doing wrong with the process?
 
Any method whose name starts with "Begin" is generally asynchronous, and BeginOutputReadLine is one such method. An asynchronous method is one that returns immediately while the operation it began carries on in the background. As such, the OutputDataReceived event is going to be raised on a background thread. You cannot safely interact with the UI on a secondary thread, and you're trying to add an item to a ListBox. I'll wager that that's where your problem lies.

You should marshal a method call back to the UI thread in order to update your ListBox. For an example of how to do that:

Accessing Controls from Worker Threads
 
Even if I strip out the whole listbox reference it bombs on me; even if I do nothing with the data. I am going to try as you have suggested and see if that helps.
 
Back
Top