473,473 Members | 1,812 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Running console process

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
Nov 20 '05 #1
2 1403
* "Dave Taylor" <no*********@processeng.com> scripsit:
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.

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
Nov 20 '05 #2
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

"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:2l************@uni-berlin.de...
* "Dave Taylor" <no*********@processeng.com> scripsit:
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.

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>

Nov 20 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Jacek | last post by:
Hello! My application has to use external native library writing to stdout and stdin. My goal is to redirect output within running process (no chance to do that in child process - Process class...
2
by: Jeffrey Palermo, MCAD.Net | last post by:
I have some classes that control file processing in c#. The files come from a mainframe and may take some time for each to process. I use the Threadpool to process multiple files at once. Each...
2
by: MattMenard | last post by:
I've written a C program that I want to run inside an Gui wrapper that I started to write in MFC. I have to select some files then kick off a process running the C program. I have the initial...
4
by: sneffe | last post by:
Hi, im writing a program to keep track of which programs is currently running. I would like to have the caption of running programs displayed in a textbox on my form. I cant seem to find an easy way...
2
by: fireloard | last post by:
I have console application open running that you need to press Escape on to get it to exit. Usually once a day/week etc I want to close that process and start it again. Is there any way to find a...
8
by: nadeem_far | last post by:
Hello All, These days I am working on a console application that will register for certain events and do some processing based on the event notification. Now, all the objects are being created...
5
by: plmanikandan | last post by:
Hi, I have written a win32 console application(exname1.exe) which runs for ten minutes.I am calling this exname1.exe from another exe named exname.exe using shellexecuteex.Now from exname.exe...
3
by: WP | last post by:
Hello, I have a very simple script (or would you call it a batch file?) with the following content: connect to mydb2; DROP TABLE staff_employee_address; DROP TABLE...
1
by: Claire | last post by:
Hi, I'm trying to run mysqldump.exe from my c# application, running on windows vista with visual studio 2005. The following code does not produce the output file. The same line used in a command...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.