Question

By using this code I created a HttpListner to send custom messages to browser for some sites. The code works properly and message is getting displayed . But , when I am trying to stop the Httpserver the application gets blocked on this line : _listenerThread.Join(); I am new to threading , so any help to how I can make my code work. Below is my code

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);
}

private void HandleRequests()
{
    while (_listener.IsListening)
    {

        var context = _listener.BeginGetContext(GetContextCallBack, null);

        if (0 == WaitHandle.WaitAny(new WaitHandle[] { _stop, context.AsyncWaitHandle }))
        {
            return;
        }
    }
}

private void GetContextCallBack(IAsyncResult result)
{
    try
    {
        lock(_queue)
        {
            _queue.Enqueue(_listener.EndGetContext(result));                   
            _ready.Set();
         }
    }
    catch
    { return; }

}

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();
    }
}

private void Worker(object obj)
{
    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 ex)
        {
            MessageBox.Show(ex.Message + ex.StackTrace);
        }

    }
}

public void Dispose()
{
    Stop();
}

private void Stop()
{
    try
    {
        _stop.Reset();
        _listenerThread.Join();
        foreach (Thread worker in _workers)
        {
            worker.Join();
        }
        _listener.Stop();
        App.Current.MainWindow.Close();

    }
    catch(Exception ex)
    {
        MessageBox.Show(ex.Message + ex.StackTrace);
    }
}


public void  ProcessRequest(HttpListenerContext context)
{
    HttpListenerRequest request = context.Request;
    HttpListenerResponse response = context.Response;

    System.Text.StringBuilder sb = new StringBuilder();

    sb.Append("");
    sb.Append(string.Format("HttpMethod: {0}", request.HttpMethod));
    sb.Append(string.Format("Uri:        {0}", request.Url.AbsoluteUri));
    sb.Append(string.Format("LocalPath:  {0}", request.Url.LocalPath));
    foreach (string key in request.QueryString.Keys)
    {
        sb.Append(string.Format("Query:      {0} = {1}", key, request.QueryString[key]));
    }

    sb.Append("");

    string responseString = sb.ToString();
    byte[] buffer = System.Text.Encoding.UTF8.GetBytes(responseString);
    response.ContentLength64 = buffer.Length;

    using (System.IO.Stream outputStream = response.OutputStream)
    {
        outputStream.Write(buffer, 0, buffer.Length);
    }
}

This code might help others too who are looking to send custom messages to web browser.

Was it helpful?

Solution

This is the first problem in the "stopping" thread:

_listenerThread.Join();
...
_listener.Stop();

You're waiting for the listener thread to stop before you tell the listener to stop listening... but the listener thread has:

private void HandleRequests()
{
    while (_listener.IsListening)
    {
        ...
    }
}

... so it will only terminate after the listener has stopped. You've basically got a deadlock. You should change the order of the stopping code:

_listener.Stop();
_listenerThread.Join();
...

Additionally this line:

_stop.Reset();

should be:

_stop.Set();

as you want to signal that you're stopping - not clear the signal.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top