Hello all,
I am trying to launch a process and grab the StandardOutput and StandardError using EventHandlers and BeginOutputReadLine. I can launch the process but I get
Exception calling "BeginOutputReadLine" with "0" argument(s): "Cannot mix synchronous and asynchronous operation on process stream."
Here is my launch function:
Function Launch {
if ($Global:Filename -eq "") {Throw ("Error: No Filename was provided!")}
Write-Host $Global:Filename
$ProcessStartInfo.FileName = $Global:Filename
$ProcessStartInfo.Arguments = $Global:Arguments
$ProcessStartInfo.UseShellExecute = $False
$ProcessStartInfo.RedirectStandardOutput = $true
$ProcessStartInfo.RedirectStandardError = $true
$Process = [System.Diagnostics.Process]::Start($ProcessStartInfo)
$Process.EnableRaisingEvents
$Process.add_OutputDataReceived($Function:ReadOutput)
$Process.add_ErrorDataReceived($Function::ReadError)
$StandardOutputStreamReader = $Process.StandardOutput
$StandardErrorStreamReader = $Process.StandardError
$Process.BeginOutputReadLine() # <--
$Process.BeginErrorReadLine() # <--
$Process.WaitForExit()
}
Am I violating the Powershell threading model? Thanks!