title: Asynchronous http listener c#
body: <p>I am developing a small SMS server in C# and want to make an asynchronous listener that processes requests sent to an SMS object (username, message, etc.). I want to create the asynchronous listener as a class library. Here is the code:
  </p>
  
  <pre><code>using System;
  using System.Net;
  using System.Threading;
  
  namespace AListener
  {
      public class CAListener
      {
          public CAListener()
          {
              Thread listener = new Thread(new ParameterizedThreadStart(RequestListener));
              listener.Start(new string[] { "http://*:11600/" });
          }
  
          private ManualResetEvent stopListener = new ManualResetEvent(false);
          private object lockListener = new object();
          private bool newRequest = false;
  
          public void StopListener()
          {
              stopListener.Set();
          }
  
          public void RequestListener(object parameter)
          {
              string[] prefixes = parameter as string[];
  
              HttpListener listener = new HttpListener();
  
              foreach (string prefix in prefixes)
              {
                  listener.Prefixes.Add(prefix);
              }
  
              listener.Start();
  
              IAsyncResult result = listener.BeginGetContext(new AsyncCallback(ListenerCallback), listener);
  
              while (!stopListener.WaitOne(10))
              {
                  lock (lockListener)
                  {
                      if (newRequest)
                      {
                          result = listener.BeginGetContext(new AsyncCallback(ListenerCallback), listener);
  
                          newRequest = false;
                      }
                  }
  
                  result.AsyncWaitHandle.WaitOne(10);
              }
  
              listener.Close();
          }
  
          public void ListenerCallback(IAsyncResult result)
          {
              HttpListener listener = (HttpListener)result.AsyncState;
  
              // Call EndGetContext to complete the asynchronous operation.
              HttpListenerContext context = listener.EndGetContext(result);
  
              HttpListenerRequest request = context.Request;
  
              // Obtain a response object.
              HttpListenerResponse response = context.Response;
  
              // Construct a response. 
              string responseString = "&lt;HTML&gt;&lt;BODY&gt; Hello world!&lt;/BODY&gt;&lt;/HTML&gt;";
              byte[] buffer = System.Text.Encoding.UTF8.GetBytes(responseString);
  
              // Get a response stream and write the response to it.
              response.ContentLength64 = buffer.Length;
  
              System.IO.Stream output = response.OutputStream;
  
              output.Write(buffer, 0, buffer.Length);
  
              // You must close the output stream.
              output.Close();
  
              lock (lockListener)
              {
                  newRequest = true;
              }
          }
      }
  }
  </code></pre>
  
  <p>I have a UI form where I obtain AListener object and everything here is OK.
  </p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.ComponentModel;
  using System.Data;
  using System.Drawing;
  using System.Linq;
  using System.Text;
  using System.Windows.Forms;
  using AListener;
  
  namespace ZNSMS
  {
      public partial class ZNSMSServer : Form
      {
          public ZNSMSServer()
          {
              InitializeComponent();
          }
  
          CAListener ca = null;
  
          private void button1_Click(object sender, EventArgs e)
          {
              ca.StopListener();
          }
  
          private void ZNSMSServer_Load(object sender, EventArgs e)
          {
              ca = new CAListener();
          }
      }
  }
  </code></pre>
  
  <p>But when I press the Button (I just want to stop the listener) -  for some reason <code>listener.Stop()</code> kills my main UI Form <code>ZNSMSServer</code>. When I comment <code>listener.Stop()</code> it's working, but I think that's not the proper way and I cannot understand why calling <code>listener.Stop()</code> can impact (in this case Close?) the calling object - in this case <code>ZNSMSServer</code>.</p>
  
  <p>If anyone has dealt with this kind of issue before, I would really appreciate it.
  Thanks in advance.</p>