how to read byte and write byte at the same time?

mc 4,796 Reputation points
2023-11-10T08:08:11.43+00:00

how to read byte and write byte of a stream at the same time?

when I read some bytes it will give exception because I will write bytes to the stream and the Position of the stream will be the end of the stream I can not read any data.

.NET
.NET
Microsoft Technologies based on the .NET software framework.
3,964 questions
ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,665 questions
.NET Runtime
.NET Runtime
.NET: Microsoft Technologies based on the .NET software framework.Runtime: An environment required to run apps that aren't compiled to machine language.
1,177 questions
{count} votes

Accepted answer
  1. JasonPan - MSFT 5,991 Reputation points Microsoft Vendor
    2023-11-13T09:29:20.72+00:00

    Dear @mc,

    After investigating the issue deeper, I found it's UWP app, it shouldn't use ASP.NET Core Tag.
    Since I don't have the environment to test, the sample code should be like below, it can be built successfully, you can try it first.

     public class CameraStreamer
     {
         private BlockingCollection<byte[]> _buffer = new BlockingCollection<byte[]>();
         private const int BufferSize = 4096;
    
         public async Task StartRecordToStreamAsync(MediaCapture capture, MediaEncodingProfile profile, Socket socket)
         {
             var memoryStream = new MemoryStream();
             await capture.StartRecordToStreamAsync(profile, memoryStream.AsRandomAccessStream());
    
             Task.Run(() => ReadFromStreamToBuffer(memoryStream));
    
             Task.Run(() => SendFromBufferToSocket(socket));
         }
    
         private void ReadFromStreamToBuffer(MemoryStream stream)
         {
             while (true) 
             {
                 if (stream.Length > BufferSize)
                 {
                     byte[] buffer = new byte[BufferSize];
                     stream.Read(buffer, 0, BufferSize);
                     _buffer.Add(buffer);
                 }
             }
         }
    
         private async Task SendFromBufferToSocket(Socket socket)
         {
             NetworkStream networkStream = new NetworkStream(socket);
             foreach (var buffer in _buffer.GetConsumingEnumerable())
             {
                 await networkStream.WriteAsync(buffer, 0, buffer.Length);
             }
         }
     }
    

    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Best regards,

    Jason


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.