title: How to cancel NetworkStream.ReadAsync without closing stream
body: <p>I am trying to use NetworkStream.ReadAsync() to read data but I cannot find how to cancel the ReadAsync() once called.  For background, the NetworkStream is provided to me by a connected BluetoothClient object (from 32Feet.NET Bluetooth Library).</p>
  
  <p>The current get-it-working code I'm trying is below.</p>
  
  <pre><code>int bytesRead;
  
  while (this.continueReading)
  {
      bytesRead = await this.stream.ReadAsync(this.buffer, 0, (int)this.buffer.Length);
  
      Console.WriteLine("Received {0} bytes", bytesRead);
  }
  
  Console.WriteLine("Receive loop has ended");
  </code></pre>
  
  <p>The code works fine receiving data, and will stop looping if the continueReading flag is set to false and data is received, but until data is received it will not proceed past the ReadAsync() line. I cannot see how to abort the call without receiving data.</p>
  
  <p>I am aware that there is an overload of ReadAsync which provides a CancellationToken, but it appears that as NetworkStream doesn't override the default ReadAsync behaviour, the token is ignored (see <a href="https://stackoverflow.com/questions/12421989/networkstream-readasync-with-a-cancellation-token-never-cancels">NetworkStream.ReadAsync with a cancellation token never cancels</a>).</p>
  
  <p>I've tried closing the underlying stream, and this causes the waiting ReadAsync call to throw ObjectDisposedException, and the underlying Bluetooth connection gets closed too. Ideally I don't want to completely sever the connection with the device just to stop reading.  It doesn't seem a clean way of doing it, and feels unnecessary to tear down the entire stream just to interrupt va ReadAsync() call.</p>
  
  <p>Any advice?</p>