Multi-threading with .Net HttpListener Multi-threading with .Net HttpListener multithreading multithreading

Multi-threading with .Net HttpListener


For completeness, here is what it would look like if you manage your own worker threads:

class HttpServer : IDisposable{    private readonly HttpListener _listener;    private readonly Thread _listenerThread;    private readonly Thread[] _workers;    private readonly ManualResetEvent _stop, _ready;    private Queue<HttpListenerContext> _queue;    public HttpServer(int maxThreads)    {        _workers = new Thread[maxThreads];        _queue = new Queue<HttpListenerContext>();        _stop = new ManualResetEvent(false);        _ready = new ManualResetEvent(false);        _listener = new HttpListener();        _listenerThread = new Thread(HandleRequests);    }    public void Start(int port)    {        _listener.Prefixes.Add(String.Format(@"http://+:{0}/", port));        _listener.Start();        _listenerThread.Start();        for (int i = 0; i < _workers.Length; i++)        {            _workers[i] = new Thread(Worker);            _workers[i].Start();        }    }    public void Dispose()    { Stop(); }    public void Stop()    {        _stop.Set();        _listenerThread.Join();        foreach (Thread worker in _workers)            worker.Join();        _listener.Stop();    }    private void HandleRequests()    {        while (_listener.IsListening)        {            var context = _listener.BeginGetContext(ContextReady, null);            if (0 == WaitHandle.WaitAny(new[] { _stop, context.AsyncWaitHandle }))                return;        }    }    private void ContextReady(IAsyncResult ar)    {        try        {            lock (_queue)            {                _queue.Enqueue(_listener.EndGetContext(ar));                _ready.Set();            }        }        catch { return; }    }    private void Worker()    {        WaitHandle[] wait = new[] { _ready, _stop };        while (0 == WaitHandle.WaitAny(wait))        {            HttpListenerContext context;            lock (_queue)            {                if (_queue.Count > 0)                    context = _queue.Dequeue();                else                {                    _ready.Reset();                    continue;                }            }            try { ProcessRequest(context); }            catch (Exception e) { Console.Error.WriteLine(e); }        }    }    public event Action<HttpListenerContext> ProcessRequest;}


Well there are several ways to solve this... This is a simple example that uses a semaphore to track ongoing work, and a signal that is raised when all workers are finished. This should give you a basic idea to work from.

The solution below is not ideal, ideally we should acquire the semaphore before calling BeginGetContext. That makes shutdown more difficult, so I've chosen to use this more simplified approach. If I were doing this for 'real' I'd probably write my own thread management rather than relying on the ThreadPool. This would allow a more dependable shutdown.

Anyway here is the complete example:

class TestHttp{    static void Main()    {        using (HttpServer srvr = new HttpServer(5))        {            srvr.Start(8085);            Console.WriteLine("Press [Enter] to quit.");            Console.ReadLine();        }    }}class HttpServer : IDisposable{    private readonly int _maxThreads;    private readonly HttpListener _listener;    private readonly Thread _listenerThread;    private readonly ManualResetEvent _stop, _idle;    private readonly Semaphore _busy;    public HttpServer(int maxThreads)    {        _maxThreads = maxThreads;        _stop = new ManualResetEvent(false);        _idle = new ManualResetEvent(false);        _busy = new Semaphore(maxThreads, maxThreads);        _listener = new HttpListener();        _listenerThread = new Thread(HandleRequests);    }    public void Start(int port)    {        _listener.Prefixes.Add(String.Format(@"http://+:{0}/", port));        _listener.Start();        _listenerThread.Start();    }    public void Dispose()    { Stop(); }    public void Stop()    {        _stop.Set();        _listenerThread.Join();        _idle.Reset();        //aquire and release the semaphore to see if anyone is running, wait for idle if they are.        _busy.WaitOne();        if(_maxThreads != 1 + _busy.Release())            _idle.WaitOne();        _listener.Stop();    }    private void HandleRequests()    {        while (_listener.IsListening)        {            var context = _listener.BeginGetContext(ListenerCallback, null);            if (0 == WaitHandle.WaitAny(new[] { _stop, context.AsyncWaitHandle }))                return;        }    }    private void ListenerCallback(IAsyncResult ar)    {        _busy.WaitOne();        try        {            HttpListenerContext context;            try            { context = _listener.EndGetContext(ar); }            catch (HttpListenerException)            { return; }            if (_stop.WaitOne(0, false))                return;            Console.WriteLine("{0} {1}", context.Request.HttpMethod, context.Request.RawUrl);            context.Response.SendChunked = true;            using (TextWriter tw = new StreamWriter(context.Response.OutputStream))            {                tw.WriteLine("<html><body><h1>Hello World</h1>");                for (int i = 0; i < 5; i++)                {                    tw.WriteLine("<p>{0} @ {1}</p>", i, DateTime.Now);                    tw.Flush();                    Thread.Sleep(1000);                }                tw.WriteLine("</body></html>");            }        }        finally        {            if (_maxThreads == 1 + _busy.Release())                _idle.Set();        }    }}


I have consulted my code in EDIT part of my question and I've decided to accept it with some modifications:

public void Stop() {    lock (locker)    {        isStopping = true;    }    resetEvent.WaitOne(); //initially set to true    listener.Stop();}private void ListenerCallback(IAsyncResult ar){    lock (locker) //locking on this is a bad idea, but I forget about it before    {        if (isStopping)            return;        resetEvent.Reset();        numberOfRequests++;    }    try    {        var listener = ar.AsyncState as HttpListener;        var context = listener.EndGetContext(ar);        //do some stuff    }    finally //to make sure that bellow code will be executed    {        lock (locker)        {            if (--numberOfRequests == 0)                resetEvent.Set();        }    }}