UPDATE: The key part is that the stream we are using is too large for the buffer used by copyto\copytoasync and so rather than running the whole task asynchronously, we need to asynchronously process the stream in parts, like the question linked to below
Please excuse the VB.net code here. I also speak C# so feel free to repsond in either language.
I have been following the example here ProcessStartInfo hanging on "WaitForExit"? Why? in order to try and solve a problem we are having running out of buffer space
Previously we were trying this code:
Dim buffer As Byte() = New Byte(32767) {}
Dim file As Byte()
Using ms = New MemoryStream()
While True
Dim read As Integer = Process.StandardOutput.BaseStream.Read(buffer, 0, buffer.Length)
If read <= 0 Then
Exit While
End If
ms.Write(buffer, 0, read)
End While
file = ms.ToArray()
End Using
If Not Process.WaitForExit(timeOut) Then
Throw New Exception("Html to PDF conversion timed out.")
End If
Now I have started to convert this to the aynch approach from the linked question, but am having trouble writing to a memory stream rather than a stringbuilder. This is what I have got so far:
Dim output = new MemoryStream()
Dim errorOutput = new StringBuilder()
Using process = New Process()
Using outputWaitHandle As New AutoResetEvent(False)
Using errorWaitHandle As New AutoResetEvent(False)
process.OutputDataReceived = Function(sender, e)
If e.Data Is Nothing Then
outputWaitHandle.Set()
Else
output.Write(e.Data); //compile error here
End If
End Function
End Using
End Using
End Using
Of course e.Data is a string, but not only that I also need a buffer and an offset... and not sure what to provide here.
Any suggestions welcome, thanks!