title: How can I POST an HTTP request and wait for a callback without blocking the current thread?
body: <p>We have a .NET application that uses an HTTP based API where we <strong><code>POST</code> a request</strong> to a third party HTTP endpoint (<em>that is not under our control</em>) and <strong>it calls us back at a later time on an HTTP endpoint that we give it</strong>; something like:</p>
  
  <pre><code>WebRequest request = WebRequest.Create(urlToMethod);
  request.Method = @"POST";
  request.Headers.Add(@"Callback", "http://ourserver?id="+id ); 
  </code></pre>
  
  <p>We make thousands upon thousands of these calls and so we'd like to be as effecient as possible (in terms of speed/memory/threads etc.) </p>
  
  <p>As far as the callback code is concerned, we have a type that acts as a listener; this is how we start it up:</p>
  
  <pre><code>_httpListener = new HttpListener();
  _httpListener.Prefixes.Add(ourServer);
  _httpListener.Start();
  _httpListener.BeginGetContext(callback, null);
  </code></pre>
  
  <p>When the server calls us back, it hits our <code>callback</code> method which looks something like this:</p>
  
  <pre><code>HttpListenerContext context = _httpListener.EndGetContext(result);
  
  HttpListenerResponse httpListenerResponse = context.Response;
  httpListenerResponse.StatusCode = 200;
  httpListenerResponse.ContentLength64 = _acknowledgementBytes.Length;
  
  var output = httpListenerResponse.OutputStream;
  output.Write(_acknowledgementBytes, 0, _acknowledgementBytes.Length);
  
  context.Response.Close();
  
  var handler = ResponseReceived;
  
  if (handler != null)
  {
      handler(this, someData);
  }
  </code></pre>
  
  <p>So we have a single instance of this <em>listener</em> (_which internally uses <code>HttpListener</code>) and for every response it gets, it <strong>informs all of the subscribers on the <code>ResponseReceived</code> event</strong>.</p>
  
  <p>The subscribers (possibly hundreds of them) only care about data associated with their particular <code>id</code>.  The <code>subscriber</code>s look something like:</p>
  
  <pre><code>_matchingResponseReceived = new ManualResetEventSlim(false);
  _listener.WhenResponseReceived += checkTheIdOfWhatWeGetAndSetTheEventIfItMatches;
  postTheMessage();
  _matchingResponseReceived.Wait(someTimeout);
  </code></pre>
  
  <p>It's <strong>that last line</strong> that's bugging me. We post the message but then block the <strong>whole thread</strong> waiting for the <code>Listener</code> to get a response and call our event handler. We'd like to use <code>Task</code>s but doesn't seem like it'll give us much if we're <strong>blocking a whole thread waiting for the callback</strong>.</p>
  
  <p>Is there a better (more <strong>TPL friendly</strong>) way of achieving this so that no threads are blocked and we get fire off more requests simultaneously?</p>