M
mwazir
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.Timeout.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
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.Timeout.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