停止并重新启动 HttpListener?

Cor*_*urn 4 c# multithreading httplistener

我正在开发一个具有HttpListener. 我的目标是让用户根据自己的选择关闭和打开监听器。我将侦听器放入一个新线程中,但在中止该线程时遇到问题。我在某处读到,如果您尝试中止非托管上下文中的线程,那么一旦它重新进入托管上下文,该线程ThreadAbortException就会被解雇。看来 HttpListener 的GetContext()方法是非托管的,因为当我尝试中止线程时,直到我对我的应用程序发出 Web 请求时才会发生任何事情。然后线程退出。问题是,当我尝试终止该线程时,我可能稍后会在同一端口上再次启动该线程,并且会HttpListenerException提示前缀已注册。

如何杀死跨线程 HttpListener?是否有一个托管替代方案GetContext()可以允许线程中止?我可以以非托管代码停止的方式中止线程吗?

ebb*_*ebb 6

怎么样:

public class XListener
{
    HttpListener listener;

    public XListener(string prefix)
    {
        listener = new HttpListener();
        listener.Prefixes.Add(prefix);
    }

    public void StartListen()
    {
        if (!listener.IsListening)
        {
            listener.Start();

            Task.Factory.StartNew(async () =>
            {
                while (true) await Listen(listener);
            }, TaskCreationOptions.LongRunning);

            Console.WriteLine("Listener started");
        }
    }

    public void StopListen()
    {
        if (listener.IsListening)
        {
            listener.Stop();
            Console.WriteLine("Listener stopped");
        }
    }

    private async Task Listen(HttpListener l)
    {
        try
        {
            var ctx = await l.GetContextAsync();

            var text = "Hello World";
            var buffer = Encoding.UTF8.GetBytes(text);

            using (var response = ctx.Response)
            {
                ctx.Response.ContentLength64 = buffer.Length;
                ctx.Response.OutputStream.Write(buffer, 0, buffer.Length);
            }
        }
        catch (HttpListenerException)
        {
            Console.WriteLine("screw you guys, I'm going home!");
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

用法:

var x = new XListener("http://locahost:8080");

x.StartListen();
Thread.Sleep(500); // test purpose only

x.StopListen();
Thread.Sleep(500); // test purpose only

x.StartListen();

/* OUTPUT:
=> Listener started
=> Listener stopped
=> screw you guys, I'm going home!
=> Listener started */
Run Code Online (Sandbox Code Playgroud)