我正在开发一个应用程序,它有一个HttpListener
。我的目标是让用户根据自己的选择关闭和打开监听器。我将侦听器放入一个新线程中,但在中止该线程时遇到问题。我在某处读到,如果您尝试中止非托管上下文中的线程,那么一旦它重新进入托管上下文ThreadAbortException
将被解雇。看来 HttpListener 的GetContext()
方法是非托管的,因为当我尝试中止线程时,直到我对我的应用程序发出 Web 请求为止,不会发生任何事情。然后线程退出。问题是,当我尝试终止该线程时,我可能稍后会在同一端口上再次启动该线程,并且HttpListenerException
关闭后表示该前缀已注册。
如何杀死跨线程 HttpListener?是否有一个托管替代方案GetContext()
这将允许线程中止?我可以以非托管代码停止的方式中止线程吗?
怎么样:
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!");
}
}
}
Usage:
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 */
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)