title: MemoryStream with wait till there is something to read
body: <p>This is the very simple question, but I can not seems figure this out without talking to somebody.</p>
  
  <p>I need a Stream like MemoryStream that will asynchronously block on read till there is something to read OR timeout elapsed.</p>
  
  <p><strong>Update:</strong>
  Ok. I've gave up and wrote the Wrapper class bymyself, but EndRead always returns 0. Please look at the code below. (Do not offer the Task oriented solutions.)</p>
  
  <pre><code>    public class BlockingMemoryStream : MemoryStream
      {
          ManualResetEventSlim isReadReady = new ManualResetEventSlim(false);
  
          public override void Write(byte[] buffer, int offset, int count)
          {
              base.Write(buffer, offset, count);
              //base.Position = offset; //I do not know if I need this!!!!
              isReadReady.Set();
          }
  
          public override IAsyncResult BeginRead(byte[] buffer, int offset, int count, AsyncCallback callback, object state)
          {
              IAsyncResult result = base.BeginRead(buffer, offset, count, callback, state);
              return result;
          }
  
          public override int EndRead(IAsyncResult asyncResult)
          {
              isReadReady.Wait(/*600000*/);
              int aa = base.EndRead(asyncResult);
              return aa;
          }
      }
  </code></pre>