HttpListener Issue
I am threading an HttpListener application and running into an exception when stopping the listener.
The I/O operation has been aborted because of either a thread exit or an application request.
The error code is 995. I am not sure what I am doing wrong but could sure use some assistance.
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Text;
using System.Net;
using System.Threading;
namespace CSG.CentricInterfaces.Emulator
{
public class EmulatorServer
{
//
// Declare the listener variables.
//
HttpListener _emulatorListener;
HttpListenerContext _emulatorContext;
//
// Thread variables.
Thread _emulatorListenerThread;
string _emulatorName = string.Empty;
//
// Declare class variables.
//
string _hostPort = string.Empty;
/// <summary>
/// Default constructor that will use the localhost and port 2112
/// for the prefix arguments.
/// </summary>
public EmulatorServer()
{
//
// Default host port for Centric Interfaces emulator(s).
//
_hostPort = "http://localhost:6111/";
_emulatorName = "CentricInterfacesEmulator";
}
/// <summary>
/// Overloaded constructor that takes the host and port as
/// parameters.
/// </summary>
/// <param name="HostPort"></param>
public EmulatorServer(string HostPort, string EmulatorName)
{
//
// Set the object variable to be used.
//
_hostPort = HostPort;
_emulatorName = EmulatorName;
}
/// <summary>
///
/// </summary>
public void Start()
{
_emulatorListenerThread = new Thread(EmulatorListenerThread);
_emulatorListenerThread.Name = _emulatorName;
_emulatorListenerThread.Start();
}
/// <summary>
/// Stop the listener so GC can clean up the resources.
/// </summary>
public void Stop()
{
//
// Cleanup the listener.
//
_emulatorListener.Stop();
}
/// <summary>
///
/// </summary>
protected void EmulatorListenerThread()
{
try
{
//
// Instantiate and initialize the listener.
//
_emulatorListener = new HttpListener();
//
// Add the host and port to the prefix list.
//
_emulatorListener.Prefixes.Add(_hostPort);
//
// Start the listener.
//
_emulatorListener.Start();
//
// Setup recursive port monitoring.
//
while (true)
{
//
// Listen for the next incoming message.
//
_emulatorContext = _emulatorListener.GetContext();
}
}
catch (Exception e)
{
Console.WriteLine("Exception message: {0}", e.Message);
}
}
}
}
|