473,387 Members | 1,520 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Process StandardOutput in a seperate thread

Hi all,

I have a process thats starts in my application and only terminates when my
application is terminated. I want to write the output and the errors of this
process to a seperate log file. In order to do this, I spawned two threads.

My code looks something like this

' Starting the process
oProcessStartInfo = New ProcessStartInfo()
With oProcessStartInfo
.FileName = strFilename
.Arguments = strArguments
.UseShellExecute = False
.CreateNoWindow = True
.RedirectStandardOutput = True
.RedirectStandardError = True
End With
oProcess = Process.Start(oProcessStartInfo)
' Starting the threads
oOutputThread = New Thread(AddressOf ReadStdOut)
oErrorThread = New Thread(AddressOf ReadStdError)
With oOutputThread
.Name = "StandardOutput"
.Priority = ThreadPriority.BelowNormal
.Start()
End With

With oErrorThread
.Name = "StandardError"
.Priority = ThreadPriority.BelowNormal
.Start()
End With

Private Sub ReadStdOut()
' Has to run in a seperate thread
' ReadtoEnd will work only if the process is getting terminated.
Dim str As String = oProcess.StandardOutput.ReadLine
Try
Do While str.Length >= 0
If str.Length <> 0 Then
Me.oLog.WriteLog("Standard Output : " & str) ' Writes to
a log file.
End If
str = oProcess.StandardOutput.ReadLine
Loop
Catch
Return
End Try
End Sub

I have a similair ReadStdError function and both share the same instance of
the logging class.
When writing in the log class I implement ReadWriteLock

ReadWriteLock.AcquireWriterLock(System.Threading.T imeout.Infinite)
Try
With oStreamWriter
.BaseStream.Seek(0, SeekOrigin.End)
.WriteLine(sDate & " : " & sMessage)
End With
Finally
ReadWriteLock.ReleaseWriterLock() ' Release the write lock.
End Try
I would like to know if this is the best approach. It works well for me.
However I felt that if I have an infinite loop while reading the standard
output stream, my application would be memory and process intensive and that
hasnt been the case. So I am a bit curious to know why it hasnt been
intensive. Greateful for any thoughts you may have.

Regards,
Wazir
Nov 22 '05 #1
2 2253
Hi Mwazir,

Did you know that there is the newsgroup
microsoft.public.dotnet.languages.vb

This is a nice question for that newsgroup in my opinion.

You says it works so it is a nice piece of work.
What it does when I see it right is

Start a commandline proces.
Reads assynchroon the input for that process
Write assynchroon the output for that process.

I do not know if assynchroon use will have much benefit in this situation.

Assynchron have benefits when there are wait times in the proces, which will
spent time when they cannot be cached, otherwise they only use more
resources and time.
In this situation I do not see those wait times.

However maybe I see it wrong. As I said a good place to ask is the newsgroup
I mentionned above. However there are here also regulars who can answer that
in a more general way.

Cor


"mwazir" <mw*********@hotmail.com> schreef in bericht
news:%2****************@TK2MSFTNGP12.phx.gbl...
Hi all,

I have a process thats starts in my application and only terminates when my application is terminated. I want to write the output and the errors of this process to a seperate log file. In order to do this, I spawned two threads.
My code looks something like this

' Starting the process
oProcessStartInfo = New ProcessStartInfo()
With oProcessStartInfo
.FileName = strFilename
.Arguments = strArguments
.UseShellExecute = False
.CreateNoWindow = True
.RedirectStandardOutput = True
.RedirectStandardError = True
End With
oProcess = Process.Start(oProcessStartInfo)
' Starting the threads
oOutputThread = New Thread(AddressOf ReadStdOut)
oErrorThread = New Thread(AddressOf ReadStdError)
With oOutputThread
.Name = "StandardOutput"
.Priority = ThreadPriority.BelowNormal
.Start()
End With

With oErrorThread
.Name = "StandardError"
.Priority = ThreadPriority.BelowNormal
.Start()
End With

Private Sub ReadStdOut()
' Has to run in a seperate thread
' ReadtoEnd will work only if the process is getting terminated.
Dim str As String = oProcess.StandardOutput.ReadLine
Try
Do While str.Length >= 0
If str.Length <> 0 Then
Me.oLog.WriteLog("Standard Output : " & str) ' Writes to a log file.
End If
str = oProcess.StandardOutput.ReadLine
Loop
Catch
Return
End Try
End Sub

I have a similair ReadStdError function and both share the same instance of the logging class.
When writing in the log class I implement ReadWriteLock

ReadWriteLock.AcquireWriterLock(System.Threading.T imeout.Infinite)
Try
With oStreamWriter
.BaseStream.Seek(0, SeekOrigin.End)
.WriteLine(sDate & " : " & sMessage)
End With
Finally
ReadWriteLock.ReleaseWriterLock() ' Release the write lock.
End Try
I would like to know if this is the best approach. It works well for me.
However I felt that if I have an infinite loop while reading the standard
output stream, my application would be memory and process intensive and that hasnt been the case. So I am a bit curious to know why it hasnt been
intensive. Greateful for any thoughts you may have.

Regards,
Wazir

Nov 22 '05 #2
Cor,

I will post this again to the microsoft.public.dotnet.languages.vb group, as
advised

Many thanks for your inputs,
Wazir
Nov 22 '05 #3

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

Similar topics

2
by: mwazir | last post by:
Hi all, I have reposted this question from dotnet.general as I have been advised that this is a more appropriate forum for this question. Apologies for the repost. I have a process thats...
3
by: Al Cohen | last post by:
I'll start by warning that I'm a newbie to C# (but I've been programming for 25 years), so I may just be doing something reallyreally dumb. I'm writing a C# wrapper for a command-line application...
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...
1
by: Scott | last post by:
I am trying to write an asp.net web page that will spawn a third party .exe file and return the results back to my asp page. I am able to write a vb.net program that will do this just fine. I...
1
by: solex | last post by:
Hello All, Hopefully someone has run into this error. I have written a class(source below) that launches a thread to monitor the StandardOutput of a System.Diagnostics.Process, in particular I...
0
by: BasicQ | last post by:
I am running an executable from my aspx page with the click of a button. A date is passed as an argument. I am able to get the standardoutput from the Process(Exe) into the label of my page after...
2
by: Al | last post by:
I'm currently attempting to use PLink (the console component of PUTTY - see http://www.chiark.greenend.org.uk/~sgtatham/putty/) as a Telnet component as I may in future need to change to using SSH...
0
by: smimon | last post by:
Hi I'm trying to run a DTS package from a ASP.NET web page using System.Diagnostics.Process. This DTS takes up to 10 minutes to complete, during which, output is generated which i would like to...
5
by: =?Utf-8?B?Z215ZXJz?= | last post by:
Hello, I am attempting to start a cmd.exe process and pass several .vbs scripts (with additional parameters) and then read the output from the scripts and make "notes" in a DataTable (the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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,...

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.