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.Diagnost ics.Process, in particular I am executing the find.exe
program.
The application works perfectly in the development environment. As soon as
I execute the compiled program and start the find process I get an
"Applicatio n Error". The error states that the instruction 0x7552bb73
referenced memory at "0x00000000 0" the memor could not be read.
Has anyone seen this behavior? If so can you point me in the write direction
to a solution?
Thanks,
Dan
Imports System
Imports System.Collecti ons.Specialized
Imports System.Diagnost ics
Imports System.IO
Imports System.Threadin g
Friend Class ProcessControll er
Private mobjStartInfo As ProcessStartInf o
Private WithEvents mobjProcess As System.Diagnost ics.Process
Private mobjErrorMonito r As System.Threadin g.Thread
Private mobjOutputMonit or As System.Threadin g.Thread
Public Sub New(ByVal startInfo As ProcessStartInf o)
Me.mobjStartInf o = startInfo
End Sub
Public Sub New(ByVal filename As String, ByVal arguments As String, ByVal
environmentVari ables As StringDictionar y)
Dim fileInfo As System.IO.FileI nfo
fileInfo = New System.IO.FileI nfo(filename)
mobjStartInfo = New ProcessStartInf o
mobjStartInfo.F ileName = fileInfo.FullNa me
mobjStartInfo.W orkingDirectory = fileInfo.Direct oryName
mobjStartInfo.A rguments = arguments
If Not (environmentVar iables Is Nothing) Then
For Each var As String In environmentVari ables.Keys
If (mobjStartInfo. EnvironmentVari ables.ContainsK ey(var)) Then
mobjStartInfo.E nvironmentVaria bles(var) = environmentVari ables(var)
Else
mobjStartInfo.E nvironmentVaria bles.Add(var, environmentVari ables(var))
End If
Next
End If
mobjStartInfo.R edirectStandard Output = True
mobjStartInfo.R edirectStandard Error = True
mobjStartInfo.R edirectStandard Input = True
mobjStartInfo.U seShellExecute = False
End Sub
Public ReadOnly Property MyProcess() As Process
Get
Return (Me.mobjProcess )
End Get
End Property
Public Function Start() As Process
mobjProcess = System.Diagnost ics.Process.Sta rt(mobjStartInf o)
If (mobjProcess.St artInfo.Redirec tStandardError) Then
mobjErrorMonito r = New System.Threadin g.Thread(New
System.Threadin g.ThreadStart(A ddressOf MonitorStandard Output))
mobjErrorMonito r.Start()
End If
If (mobjProcess.St artInfo.Redirec tStandardOutput ) Then
mobjOutputMonit or = New System.Threadin g.Thread(New
System.Threadin g.ThreadStart(A ddressOf MonitorStandard Output))
mobjOutputMonit or.Start()
End If
Return mobjProcess
End Function
Public Sub [Stop]()
If Not (Me.mobjProcess .HasExited) Then
If (Me.mobjProcess .StartInfo.Redi rectStandardInp ut) Then
Me.mobjProcess. StandardInput.C lose()
End If
Me.mobjProcess. Kill()
Me.mobjProcess. WaitForExit()
Me.mobjProcess. Close()
End If
If Not (Me.mobjOutputM onitor Is Nothing) Then Me.mobjOutputMo nitor.Abort()
If Not (Me.mobjErrorMo nitor Is Nothing) Then Me.mobjErrorMon itor.Abort()
RaiseEvent Stopped()
End Sub
Public Event StandardOutputR eadLineHandler( ByVal lineText As String)
Public Event StandardErrorRe adLineHandler(B yVal lineText As String)
Public Event Stopped()
Public Event Exited()
Private Sub MonitorStandard Output()
Try
Dim line As String
line = Me.mobjProcess. StandardOutput. ReadLine()
While Not (line Is Nothing)
RaiseEvent StandardOutputR eadLineHandler( line)
line = Me.mobjProcess. StandardOutput. ReadLine()
End While
If (line Is Nothing) Then Me.Stop()
Catch ex As System.Exceptio n
Return
End Try
End Sub
Private Sub MonitorStandard Error()
Try
Dim line As String
line = Me.mobjProcess. StandardError.R eadLine()
While Not (line Is Nothing)
RaiseEvent StandardErrorRe adLineHandler(l ine)
line = Me.mobjProcess. StandardError.R eadLine()
End While
Catch ex As System.Exceptio n
Return
End Try
End Sub
Public Sub WriteStandardIn put(ByVal text As String)
Me.mobjProcess. StandardInput.W rite(text)
End Sub
Public Sub WriteStandardIn putLine(ByVal textLine As String)
Me.mobjProcess. StandardInput.W riteLine(textLi ne)
End Sub
Protected Overrides Sub Finalize()
mobjStartInfo = Nothing
mobjProcess = Nothing
mobjErrorMonito r = Nothing
mobjOutputMonit or = Nothing
MyBase.Finalize ()
End Sub
Private Sub mobjProcess_Exi ted(ByVal sender As Object, ByVal e As
System.EventArg s) Handles mobjProcess.Exi ted
RaiseEvent Exited()
End Sub