1

我正在用 C# 开发一个小型 SMS 服务器,并希望创建一个异步侦听器来处理发送到 SMS 对象(用户名、消息等)的请求。我想将异步侦听器创建为类库。这是代码:

using System;
using System.Net;
using System.Threading;

namespace AListener
{
    public class CAListener
    {
        public CAListener()
        {
            Thread listener = new Thread(new ParameterizedThreadStart(RequestListener));
            listener.Start(new string[] { "http://*:11600/" });
        }

        private ManualResetEvent stopListener = new ManualResetEvent(false);
        private object lockListener = new object();
        private bool newRequest = false;

        public void StopListener()
        {
            stopListener.Set();
        }

        public void RequestListener(object parameter)
        {
            string[] prefixes = parameter as string[];

            HttpListener listener = new HttpListener();

            foreach (string prefix in prefixes)
            {
                listener.Prefixes.Add(prefix);
            }

            listener.Start();

            IAsyncResult result = listener.BeginGetContext(new AsyncCallback(ListenerCallback), listener);

            while (!stopListener.WaitOne(10))
            {
                lock (lockListener)
                {
                    if (newRequest)
                    {
                        result = listener.BeginGetContext(new AsyncCallback(ListenerCallback), listener);

                        newRequest = false;
                    }
                }

                result.AsyncWaitHandle.WaitOne(10);
            }

            listener.Close();
        }

        public void ListenerCallback(IAsyncResult result)
        {
            HttpListener listener = (HttpListener)result.AsyncState;

            // Call EndGetContext to complete the asynchronous operation.
            HttpListenerContext context = listener.EndGetContext(result);

            HttpListenerRequest request = context.Request;

            // Obtain a response object.
            HttpListenerResponse response = context.Response;

            // Construct a response. 
            string responseString = "<HTML><BODY> Hello world!</BODY></HTML>";
            byte[] buffer = System.Text.Encoding.UTF8.GetBytes(responseString);

            // Get a response stream and write the response to it.
            response.ContentLength64 = buffer.Length;

            System.IO.Stream output = response.OutputStream;

            output.Write(buffer, 0, buffer.Length);

            // You must close the output stream.
            output.Close();

            lock (lockListener)
            {
                newRequest = true;
            }
        }
    }
}

我有一个 UI 表单,我在其中获取 AListener 对象,这里的一切都很好。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using AListener;

namespace ZNSMS
{
    public partial class ZNSMSServer : Form
    {
        public ZNSMSServer()
        {
            InitializeComponent();
        }

        CAListener ca = null;

        private void button1_Click(object sender, EventArgs e)
        {
            ca.StopListener();
        }

        private void ZNSMSServer_Load(object sender, EventArgs e)
        {
            ca = new CAListener();
        }
    }
}

但是当我按下 Button 时(我只想停止监听器) - 出于某种原因会listener.Stop()杀死我的主 UI Form ZNSMSServer。当我评论listener.Stop()它正在工作时,但我认为这不是正确的方法,我无法理解为什么调用listener.Stop()会影响(在这种情况下关闭?)调用对象 - 在这种情况下ZNSMSServer

如果有人以前处理过此类问题,我将不胜感激。提前致谢。

4

0 回答 0