Hi there,
My requirement is getting output from command window asynchonously in vb.net form, I am doing this with system diagnostic process and using addhandler.
My problem is, I have separate class to execute process, after process start when control goes to a addhandler sub routine, it stops there only after it's execution(in addhandler subroutine), and never comes back where I am capturing output. Control should come to the line in below code - addInput = outputExe.ToString() so that I can captureoutput and go back to vb.net code behind. I know something is wrong but not sure what it is.
code is as below
Public Class RunExe
Private Function ExecuteProcess()
procInfo = New ProcessStartInfo
procInfo.FileName = ExeFile
'If false,then the cmd window will be displayed in system while processing
procInfo.CreateNoWindow = False
procInfo.WorkingDirectory = ExePath
procInfo.WindowStyle = _
ProcessWindowStyle.Normal
' Set UseShellExecute to false for redirection.
procInfo.UseShellExecute = False
' Initialize the process and its StartInfo properties.
process.StartInfo = procInfo
' allow the process to raise events
process.EnableRaisingEvents = True
' Set event handler to asynchronously read the output.
AddHandler process.OutputDataReceived, AddressOf StandardOutputHandler
AddHandler process.ErrorDataReceived, AddressOf StandardErrorHandler
'Redirect standard input,output,error as well. This stream is used synchronously.
procInfo.RedirectStandardInput = True
procInfo.RedirectStandardOutput = True
procInfo.RedirectStandardError = True
' Start the process.
process.Start()
' Start the asynchronous read of the output stream.
process.BeginOutputReadLine()
process.BeginErrorReadLine()
' additionalInput = process.StandardOutput.ReadToEnd
' errorExe = process.StandardError.ReadToEnd
process.WaitForExit()
addInput = outputExe.ToString()
If String.IsNullOrEmpty(additionalInput) = False Then
If additionalInput.Contains("getbrgdrwg") Then
flag = False
End If
End If
end function
Private Shared Sub StandardOutputHandler( _
ByVal sendingProcess As Object, ByVal outLine As DataReceivedEventArgs)
' If Not String.IsNullOrEmpty(outLine.Data) Then Lines.Append(vbCrLf & outLine.Data)
' Collect the sort command output.
If Not String.IsNullOrEmpty(outLine.Data) Then numOutputLines += 1
' Add the text to the collected output.
outputExe.Append(Environment.NewLine + "[" + numOutputLines.ToString() + "] - " + outLine.Data)
End Sub
end class
My requirement is getting output from command window asynchonously in vb.net form, I am doing this with system diagnostic process and using addhandler.
My problem is, I have separate class to execute process, after process start when control goes to a addhandler sub routine, it stops there only after it's execution(in addhandler subroutine), and never comes back where I am capturing output. Control should come to the line in below code - addInput = outputExe.ToString() so that I can captureoutput and go back to vb.net code behind. I know something is wrong but not sure what it is.
code is as below
Public Class RunExe
Private Function ExecuteProcess()
procInfo = New ProcessStartInfo
procInfo.FileName = ExeFile
'If false,then the cmd window will be displayed in system while processing
procInfo.CreateNoWindow = False
procInfo.WorkingDirectory = ExePath
procInfo.WindowStyle = _
ProcessWindowStyle.Normal
' Set UseShellExecute to false for redirection.
procInfo.UseShellExecute = False
' Initialize the process and its StartInfo properties.
process.StartInfo = procInfo
' allow the process to raise events
process.EnableRaisingEvents = True
' Set event handler to asynchronously read the output.
AddHandler process.OutputDataReceived, AddressOf StandardOutputHandler
AddHandler process.ErrorDataReceived, AddressOf StandardErrorHandler
'Redirect standard input,output,error as well. This stream is used synchronously.
procInfo.RedirectStandardInput = True
procInfo.RedirectStandardOutput = True
procInfo.RedirectStandardError = True
' Start the process.
process.Start()
' Start the asynchronous read of the output stream.
process.BeginOutputReadLine()
process.BeginErrorReadLine()
' additionalInput = process.StandardOutput.ReadToEnd
' errorExe = process.StandardError.ReadToEnd
process.WaitForExit()
addInput = outputExe.ToString()
If String.IsNullOrEmpty(additionalInput) = False Then
If additionalInput.Contains("getbrgdrwg") Then
flag = False
End If
End If
end function
Private Shared Sub StandardOutputHandler( _
ByVal sendingProcess As Object, ByVal outLine As DataReceivedEventArgs)
' If Not String.IsNullOrEmpty(outLine.Data) Then Lines.Append(vbCrLf & outLine.Data)
' Collect the sort command output.
If Not String.IsNullOrEmpty(outLine.Data) Then numOutputLines += 1
' Add the text to the collected output.
outputExe.Append(Environment.NewLine + "[" + numOutputLines.ToString() + "] - " + outLine.Data)
End Sub
end class