Running console process

D

Dave Taylor

I'm writing an app in VB.NET that provides a user-interface to computer
simulation model that runs as a console process. I would like to have the
console output redirected to my VB.NET program and I've done so with the
attached code. The problem is, redirecting its output slows down the code
severely. The code that starts the process is launched on its own thread,
so it doesnt completely stop the interface, but the time the model takes to
run is 8x or 10x longer than running the model directly from the command
line. I thought it would be smart to have the interface program wait awhile
before reading the output, so I tried using the Thread.Sleep command,
however, because of blocking this only exacerbated the problem. Ideally,
the output would fill a textbox as it runs...but I need some pointers on how
to write this code so that it doesnt block the executing process.

Thanks

Dave Taylor

Public Sub RunPhysica()

Dim phyProcess As Process, si As ProcessStartInfo

Dim sd As System.Collections.Specialized.StringDictionary

Dim srOut As System.IO.StreamReader

Dim srErr As System.IO.StreamReader

Dim sz As String

btnRun.Enabled = False

btnOk.Enabled = False

If ckWriteGeo.Checked Then _sim.GenerateGeometry(ckCompileGeo.Checked)

If ckWriteInf.Checked Then _sim.GenerateInform(ckCompileInf.Checked)

phyProcess = New Process

si = phyProcess.StartInfo

si.FileName = App.ColumnSimulations.PhysicaExec

si.UseShellExecute = False

si.WorkingDirectory = App.WorkingDirectory

sd = si.EnvironmentVariables

sd.Add("FGVKEY", App.GetSetting("PhysicaKeyFile", "C:\Program
Files\Femsys\KeyFile"))

si.RedirectStandardOutput = True

si.RedirectStandardError = True

tbRun.SelectedTab = tbpOutput

phyProcess.Start()

srOut = phyProcess.StandardOutput

srErr = phyProcess.StandardError

sz = String.Empty : txOutput.Text = ""

While Not sz Is Nothing

sz &= srOut.ReadLine

End While

txOutput.Text = sz

txErrors.Text = srErr.ReadToEnd

btnOk.Enabled = True

btnRun.Enabled = True

End Sub
 
H

Herfried K. Wagner [MVP]

* "Dave Taylor said:
I'm writing an app in VB.NET that provides a user-interface to computer
simulation model that runs as a console process. I would like to have the
console output redirected to my VB.NET program and I've done so with the
attached code. The problem is, redirecting its output slows down the code
severely. The code that starts the process is launched on its own thread,
so it doesnt completely stop the interface, but the time the model takes to
run is 8x or 10x longer than running the model directly from the command
line. I thought it would be smart to have the interface program wait awhile
before reading the output, so I tried using the Thread.Sleep command,

/Never/ access Windows Forms controls from another thread directly.
Instance members of Windows Forms controls are not thread-safe.
Instead, use 'Control.Invoke' or 'Control.BeginInvoke' to access the
controls in the main UI thread.
 
D

Dave Taylor

Herfried,

THANK YOU! After poking around newsgroups this morning looking at the Invoke
method, it now runs nearly full speed while dumping its output to the text
box!

The updated and working code is shown below. Again, many thanks for your
advice!!!!

Public Sub RunPhysica()

Dim phyProcess As Process, si As ProcessStartInfo

Dim sd As System.Collections.Specialized.StringDictionary

Dim srOut As System.IO.StreamReader

Dim srErr As System.IO.StreamReader

Dim sz As String



phyProcess = New Process

si = phyProcess.StartInfo

si.FileName = App.ColumnSimulations.PhysicaExec

si.UseShellExecute = False

si.WorkingDirectory = App.WorkingDirectory

sd = si.EnvironmentVariables

sd.Add("FGVKEY", App.GetSetting("PhysicaKeyFile", "C:\Program
Files\Femsys\KeyFile"))

si.RedirectStandardOutput = True

si.RedirectStandardError = True

phyProcess.Start()

srOut = phyProcess.StandardOutput

srErr = phyProcess.StandardError

sz = String.Empty

While Not sz Is Nothing

sz = srOut.ReadLine & vbCrLf

If Not (sz Is Nothing) Then txOutput.Invoke(New
AppendTextDelegate(AddressOf AppendText), New Object() {txOutput, sz})

End While

sz = srErr.ReadToEnd

txErrors.Invoke(New AppendTextDelegate(AddressOf AppendText), New
Object() {txErrors, sz})

btnOk.Invoke(New SetEnabledDelegate(AddressOf SetEnabled), New Object()
{btnOk, True})

btnRun.Invoke(New SetEnabledDelegate(AddressOf SetEnabled), New Object()
{btnRun, True})

End Sub
 

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