title: Threading and blocking I/O in Windows Store app
body: <p>I want to write a server application in Windows Store app so that I can stream data from my computer to my phone and vice versa directly via my wireless network without having to plug it in via USB cable (or uploading to then downloading from the cloud). To that end, I need to create a server on my phone side. My current set up is:</p>
  
  <pre><code>StreamSocket^ socket = ref new StreamSocket();
  // Assuming that I already have a local server on PC side
  HostName^ hostname = ref new HostName("IP_OF_MY_PC");
  create_task(socket-&gt;ConnectAsync(hostname, "6666", SocketProtectionLevel::PlainSocket))
  .then([this]()
  {
      // 1. WHAT TO DO HERE TO LAUNCH A THREAD?
      // 2. HOW TO DO BLOCKING I/O IN THAT THREAD?
  });
  </code></pre>
  
  <p>I know that in the Lambda in <code>then()</code>, I can exchange data with my PC via <code>socket-&gt;InputStream</code> and <code>socket-&gt;OutputStream</code>. Unfortunately, those usually work in one-shot settings. I want to start <strong>two dedicated background threads</strong>: one listens to requests coming from my PC and the other streams back the requested files. So far, my search has been futile. So is there any relevant reference?</p>
  
  <p>As a related question, it seems that Windows Store API does not provide any mechanism for <strong>blocking I/O</strong>. At the moment, I am using</p>
  
  <pre><code> while (1)
   {
       create_task(socket-&gt;InputStream-&gt;ReadAsync(buffer, length, 
            InputStreamOptions::ReadAhead)).get();
       // process the data obtained
   }
  </code></pre>
  
  <p>but this is undesirable because we not only create new &amp; unnecessary task objects but also seem to create new threads every time I want to read data from the stream! If I already put all my I/O off the UI thread, shouldn't I have the option to do blocking I/O then? Or maybe, are there an alternative to <code>StreamSocket</code>?</p>