title: Read from process stdout into memory stream asynchronously
body: <p><strong>UPDATE:</strong> 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</p>
  
  <p>Please excuse the VB.net code here. I also speak C# so feel free to repsond in either language.</p>
  
  <p>I have been following the example here <a href="https://stackoverflow.com/questions/139593/processstartinfo-hanging-on-waitforexit-why">ProcessStartInfo hanging on &quot;WaitForExit&quot;? Why?</a> in order to try and solve a problem we are having running out of buffer space</p>
  
  <p>Previously we were trying this code:</p>
  
  <pre><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 &lt;= 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
  </code></pre>
  
  <p>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:</p>
  
  <pre><code>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
  </code></pre>
  
  <p>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.</p>
  
  <p>Any suggestions welcome, thanks!</p>