HttpListner在执行线程时停止.Join()
本文关键字:Join 执行 线程 HttpListner | 更新日期: 2023-09-27 18:21:06
通过使用此代码,我创建了一个HttpListner,用于向某些网站的浏览器发送自定义消息。代码工作正常,并且正在显示消息。但是,当我试图停止Httpserver时,应用程序在这一行被阻止:_listenerThread.Join();
我是线程的新手,所以任何帮助我如何使我的代码工作。下面是我的代码
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);
}
}
此代码可能也有助于其他希望向web浏览器发送自定义消息的人。
这是"停止"线程中的第一个问题:
_listenerThread.Join();
...
_listener.Stop();
在告诉侦听器停止侦听之前,您正在等待侦听器线程停止。。。但是侦听线程具有:
private void HandleRequests()
{
while (_listener.IsListening)
{
...
}
}
因此它只会在侦听器停止后终止。你基本上陷入了僵局。您应该更改停止代码的顺序:
_listener.Stop();
_listenerThread.Join();
...
另外这一行:
_stop.Reset();
应该是:
_stop.Set();
当您想发出信号表示您正在停止时,请不要清除该信号。