System.Diagnostics.Process

S

solex

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 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
"Application Error". The error states that the instruction 0x7552bb73
referenced memory at "0x000000000" 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.Collections.Specialized
Imports System.Diagnostics
Imports System.IO
Imports System.Threading
Friend Class ProcessController
Private mobjStartInfo As ProcessStartInfo
Private WithEvents mobjProcess As System.Diagnostics.Process
Private mobjErrorMonitor As System.Threading.Thread
Private mobjOutputMonitor As System.Threading.Thread
Public Sub New(ByVal startInfo As ProcessStartInfo)
Me.mobjStartInfo = startInfo
End Sub
Public Sub New(ByVal filename As String, ByVal arguments As String, ByVal
environmentVariables As StringDictionary)
Dim fileInfo As System.IO.FileInfo
fileInfo = New System.IO.FileInfo(filename)
mobjStartInfo = New ProcessStartInfo
mobjStartInfo.FileName = fileInfo.FullName
mobjStartInfo.WorkingDirectory = fileInfo.DirectoryName
mobjStartInfo.Arguments = arguments
If Not (environmentVariables Is Nothing) Then
For Each var As String In environmentVariables.Keys
If (mobjStartInfo.EnvironmentVariables.ContainsKey(var)) Then
mobjStartInfo.EnvironmentVariables(var) = environmentVariables(var)
Else
mobjStartInfo.EnvironmentVariables.Add(var, environmentVariables(var))
End If
Next
End If
mobjStartInfo.RedirectStandardOutput = True
mobjStartInfo.RedirectStandardError = True
mobjStartInfo.RedirectStandardInput = True
mobjStartInfo.UseShellExecute = False
End Sub
Public ReadOnly Property MyProcess() As Process
Get
Return (Me.mobjProcess)
End Get
End Property
Public Function Start() As Process
mobjProcess = System.Diagnostics.Process.Start(mobjStartInfo)
If (mobjProcess.StartInfo.RedirectStandardError) Then
mobjErrorMonitor = New System.Threading.Thread(New
System.Threading.ThreadStart(AddressOf MonitorStandardOutput))
mobjErrorMonitor.Start()
End If
If (mobjProcess.StartInfo.RedirectStandardOutput) Then
mobjOutputMonitor = New System.Threading.Thread(New
System.Threading.ThreadStart(AddressOf MonitorStandardOutput))
mobjOutputMonitor.Start()
End If
Return mobjProcess
End Function
Public Sub [Stop]()
If Not (Me.mobjProcess.HasExited) Then
If (Me.mobjProcess.StartInfo.RedirectStandardInput) Then
Me.mobjProcess.StandardInput.Close()
End If
Me.mobjProcess.Kill()
Me.mobjProcess.WaitForExit()
Me.mobjProcess.Close()
End If
If Not (Me.mobjOutputMonitor Is Nothing) Then Me.mobjOutputMonitor.Abort()
If Not (Me.mobjErrorMonitor Is Nothing) Then Me.mobjErrorMonitor.Abort()
RaiseEvent Stopped()
End Sub
Public Event StandardOutputReadLineHandler(ByVal lineText As String)
Public Event StandardErrorReadLineHandler(ByVal lineText As String)
Public Event Stopped()
Public Event Exited()
Private Sub MonitorStandardOutput()
Try
Dim line As String
line = Me.mobjProcess.StandardOutput.ReadLine()
While Not (line Is Nothing)
RaiseEvent StandardOutputReadLineHandler(line)
line = Me.mobjProcess.StandardOutput.ReadLine()
End While
If (line Is Nothing) Then Me.Stop()
Catch ex As System.Exception
Return
End Try
End Sub
Private Sub MonitorStandardError()
Try
Dim line As String
line = Me.mobjProcess.StandardError.ReadLine()
While Not (line Is Nothing)
RaiseEvent StandardErrorReadLineHandler(line)
line = Me.mobjProcess.StandardError.ReadLine()
End While
Catch ex As System.Exception
Return
End Try
End Sub
Public Sub WriteStandardInput(ByVal text As String)
Me.mobjProcess.StandardInput.Write(text)
End Sub
Public Sub WriteStandardInputLine(ByVal textLine As String)
Me.mobjProcess.StandardInput.WriteLine(textLine)
End Sub
Protected Overrides Sub Finalize()
mobjStartInfo = Nothing
mobjProcess = Nothing
mobjErrorMonitor = Nothing
mobjOutputMonitor = Nothing
MyBase.Finalize()
End Sub
Private Sub mobjProcess_Exited(ByVal sender As Object, ByVal e As
System.EventArgs) Handles mobjProcess.Exited
RaiseEvent Exited()
End Sub
 
H

Herfried K. Wagner [MVP]

* "solex said:
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 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
"Application Error". The error states that the instruction 0x7552bb73
referenced memory at "0x000000000" the memor could not be read.

Does it work when using this code?

<URL:http://dotnet.mvps.org/dotnet/samples/miscsamples/downloads/RedirectConsole.zip>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top