Process StandardOutput and StandardError on seperate threads.

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • mwazir

    Process StandardOutput and StandardError on seperate threads.

    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 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
    oProcessStartIn fo = New ProcessStartInf o()
    With oProcessStartIn fo
    .FileName = strFilename
    .Arguments = strArguments
    .UseShellExecut e = False
    .CreateNoWindow = True
    .RedirectStanda rdOutput = True
    .RedirectStanda rdError = True
    End With
    oProcess = Process.Start(o ProcessStartInf o)


    ' Starting the threads
    oOutputThread = New Thread(AddressO f ReadStdOut)
    oErrorThread = New Thread(AddressO f ReadStdError)
    With oOutputThread
    .Name = "StandardOutput "
    .Priority = ThreadPriority. BelowNormal
    .Start()
    End With

    With oErrorThread
    .Name = "StandardEr ror"
    .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.Standa rdOutput.ReadLi ne
    Try
    Do While str.Length >= 0
    If str.Length <> 0 Then
    Me.oLog.WriteLo g("Standard Output : " & str) ' Writes to
    a log file.
    End If
    str = oProcess.Standa rdOutput.ReadLi ne
    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.A cquireWriterLoc k(System.Thread ing.Timeout.Inf inite)
    Try
    With oStreamWriter
    .BaseStream.See k(0, SeekOrigin.End)
    .WriteLine(sDat e & " : " & sMessage)
    End With
    Finally
    ReadWriteLock.R eleaseWriterLoc k() ' 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.

    Couple of additional points (not sure of its impact to my question)
    - The process doesnt has a irregular stream of data coming from Standard
    Output and its not very oft occuring.
    - The StandardError by its very nature also doesnt have regular data coming
    from it unless there is a real issue. I dont expect any errors if my setups
    are correct.
    - The Do While Loop should constantly be checking ReadLine - and thats my
    real concern

    Greateful for any thoughts you may have. I am a complete greenhorn in
    threading.

    Regards,
    Wazir




  • Lifeng Lu

    #2
    RE: Process StandardOutput and StandardError on seperate threads.

    You don't have to worry about this. A thread could cause problems if it is
    continuely doing something without going to sleep mode. For example, if you
    have a loop to check a value of a variable, and exits until it changes. It
    will cause problems because the thread will consume the CPU and does
    nothing.
    In your case, your thread will sleep when there is nothing to read when you
    call ReadLine. The thread would wake up and run until something ready to
    read...

    Thanks

    Comment

    Working...