title: Simple Task-returning Asynchronous HtppListener with async/await and handling high load
body: <p>I have created the following simple <code>HttpListener</code> to serve multiple requests at the same time (on .NET 4.5):</p>
  
  <pre><code>class Program {
  
      static void Main(string[] args) {
  
          HttpListener listener = new HttpListener();
          listener.Prefixes.Add("http://+:8088/");
          listener.Start();
          ProcessAsync(listener).ContinueWith(task =&gt; { });
          Console.ReadLine();
      }
  
      static async Task ProcessAsync(HttpListener listener) {
  
          HttpListenerContext ctx = await listener.GetContextAsync();
  
          // spin up another listener
          Task.Factory.StartNew(() =&gt; ProcessAsync(listener));
  
          // Simulate long running operation
          Thread.Sleep(1000);
  
          // Perform
          Perform(ctx);
  
          await ProcessAsync(listener);
      }
  
      static void Perform(HttpListenerContext ctx) {
  
          HttpListenerResponse response = ctx.Response;
          string responseString = "&lt;HTML&gt;&lt;BODY&gt; Hello world!&lt;/BODY&gt;&lt;/HTML&gt;";
          byte[] buffer = Encoding.UTF8.GetBytes(responseString);
  
          // Get a response stream and write the response to it.
          response.ContentLength64 = buffer.Length;
          Stream output = response.OutputStream;
          output.Write(buffer, 0, buffer.Length);
  
          // You must close the output stream.
          output.Close();
      }
  }
  </code></pre>
  
  <p>I use Apache Benchmark Tool to load test this. When I make a 1 request, I get the max wait time for a request as 1 second. If I make 10 requests, for example, max wait time for a response goes up to 2 seconds.</p>
  
  <p>How would you change my above code to make it as efficient as it can be?</p>
  
  <p><strong>Edit</strong></p>
  
  <p>After @JonSkeet's answer, I changed the code as below. Initially, I tried to simulate a blocking call but I guess it was the core problem. So,I took @JonSkeet's suggestion and change that to Task.Delay(1000). Now, the below code gives max. wait time as approx. 1 sec for 10 concurrent requests:</p>
  
  <pre><code>class Program {
  
      static bool KeepGoing = true;
      static List&lt;Task&gt; OngoingTasks = new List&lt;Task&gt;();
  
      static void Main(string[] args) {
  
          HttpListener listener = new HttpListener();
          listener.Prefixes.Add("http://+:8088/");
          listener.Start();
          ProcessAsync(listener).ContinueWith(async task =&gt; {
  
              await Task.WhenAll(OngoingTasks.ToArray());
          });
  
          var cmd = Console.ReadLine();
  
          if (cmd.Equals("q", StringComparison.OrdinalIgnoreCase)) {
              KeepGoing = false;
          }
  
          Console.ReadLine();
      }
  
      static async Task ProcessAsync(HttpListener listener) {
  
          while (KeepGoing) {
              HttpListenerContext context = await listener.GetContextAsync();
              HandleRequestAsync(context);
  
              // TODO: figure out the best way add ongoing tasks to OngoingTasks.
          }
      }
  
      static async Task HandleRequestAsync(HttpListenerContext context) {
  
          // Do processing here, possibly affecting KeepGoing to make the 
          // server shut down.
  
          await Task.Delay(1000);
          Perform(context);
      }
  
      static void Perform(HttpListenerContext ctx) {
  
          HttpListenerResponse response = ctx.Response;
          string responseString = "&lt;HTML&gt;&lt;BODY&gt; Hello world!&lt;/BODY&gt;&lt;/HTML&gt;";
          byte[] buffer = Encoding.UTF8.GetBytes(responseString);
  
          // Get a response stream and write the response to it.
          response.ContentLength64 = buffer.Length;
          Stream output = response.OutputStream;
          output.Write(buffer, 0, buffer.Length);
  
          // You must close the output stream.
          output.Close();
      }
  }
  </code></pre>