0

I have read this question/answer: How do I copy the contents of one stream to another?. And I've read the doc pages of Stream.CopyToAsync and Stream.CopyTo.

I haven't found anything in any of them which either:

  • Tells me how to achieve this.
  • Confirms whether those methods do this automatically.

So it's possible that the answer is "yes, just use those methods", but that's not something that I can see documented on any of those...


  1. I have an existing Stream object (A).
  2. I have an async void method PopulateStream() which is going to write data into that stream.
  3. I want to create a new Stream object (B), and I want the data from above to be written onto that Stream.
  4. I don't want to wait for all the data to have been written to A before it starts being written to B.
    • i.e. I want the data to "Stream" from one Stream object to the other, as it is written.
  5. I don't have any control over the creation of Stream A, though I do have a reference to it.
  6. I don't have any control over the body of PopulateStream()

Without point 4. I could just await PopulateStream(), and the use the copy methods.

Can I just call the copy method before I await PopulateStream()? Will that copy future data added to Stream A? Or will it only copy the data that's currently there (which in this case would be ... nothing, yet.

If I can do this, then do I need to do anything to Stream B after PopulateStream() completes, to indicate that all the data has been written? Presumably something in PopulateStream() already does that for Stream A?

Brondahl
  • 7,402
  • 5
  • 45
  • 74
  • Problem is windows stream have only one pointer to current location. Linux streams have two pointes (one for input and one for output). So in windows you can only read or write a stream and not both at same time. – jdweng Jun 27 '22 at 10:27
  • It is based on the implemention of stream A (and PopulateStream). Stream A should be a bidirectional stream (e.g. PipeStream). – shingo Jun 27 '22 at 11:17

0 Answers0