title: Burst Mode of Request with EventHub and Async
body: <p>i am trying to send a audio file(<code>wav</code>) from a <code>IoT</code> Device to <code>Eventhub</code>. As <code>Eventhub</code> has size restriction of 64Kb per message ,each message is chunked into a <code>7kb</code> byte array and sent to <code>Eventhub</code>. i am trying to achieve maximum send rate <em>[not crossing the threshold]</em> from client.</p>
  
  <p>Recording a live audio , save it in file stream and chunk it for sending. i avoided this part with custom stream implementation</p>
  
  <pre><code>public class CustomAudioStream : IRandomAccessStream{
      public IAsyncOperationWithProgress&lt;uint, uint&gt; WriteAsync(IBuffer buffer)
          {
              return AsyncInfo.Run&lt;uint, uint&gt;((token, progress) =&gt;
               {
                   return Task.Run(() =&gt;
                   {
                       using (var memoryStream = new MemoryStream())
                       {
                           using (var outputStream = memoryStream.AsOutputStream())
                           {
                               outputStream.WriteAsync(buffer).AsTask().Wait();
  
                               var byteArray = memoryStream.ToArray();
  
                               //bytes are ready to send from here 
                               ChunkedEventSender(this, byteArray);
  #if DEBUG
                               Debug.WriteLine("Array Length:  " + byteArray.Length + "    MemoryStream length:" + memoryStream.Length);
  #endif 
                               return (uint)memoryStream.Length;
                           }
                       }
                   });
               });
          }
     }
  </code></pre>
  
  <p>But i am not able to send the byte at same speed here with REST implementation. for Sending i am using a third party wrapper so i can't do at that side. </p>
  
  <p>But i can span thread to make application responsive while interacting  so i use</p>
  
  <pre><code> Task.Factory.StartNew(()=&gt;{ 
            BackgroundSender(byte[data]);
          });
  </code></pre>
  
  <p>i don't want to wait for Task to complete neither the outcome of Task.but when i do this most of the my request are getting "<code>Request Timed out</code>" and Application is getting stuck because of those request.</p>
  
  <p>Is there anyway i can make application responsive without blocking the thread. </p>
  
  <hr>
  
  <p>Edit: Currently Applied <code>Parallel.Invoke</code> and not loosing a single message and application is also responsive but sending drop drastically to 2-3 message/Second</p>
  
  <p>Should i switch to Multi Thread model rather than using Async. Found Simlar Question like this <a href="https://stackoverflow.com/questions/16194054/is-async-httpclient-from-net-4-5-a-bad-choice-for-intensive-load-applications?rq=1">Is async HttpClient from .Net 4.5 a bad choice for intensive load applications?</a></p>