0

我有以下方法启动我的服务器:

    public void EngageServer()
    {
        this.tcpListener = new TcpListener(IPAddress.Any, _InternalPort);
        this.listenThread = new Thread(RunServer);
        this.listenThread.Start();

        Thread t = new Thread(new ParameterizedThreadStart(CheckPortStatus));
        t.Start(_ExternalPort);
    }

问题是线程t一直在运行,即使工作完成后,这里是方法:

private void CheckPortStatus(object portObject)
{
    /*
     * For information about this method functionality 
     * please refer to the information file attached to this project on the folder "Information"
     */
    int port = (int)portObject;
    ComputerInfo computerInfo = ComputerInfo.Instance;
    if (computerInfo.isNetworkAvailable())
    {
        using (WebClient browser = new WebClient())
        {
            string content = browser.DownloadString(Properties.Settings.Default.CheckPortURL.Replace("{0}", port.ToString()));
            if (content.Contains("is responding"))
            {
                InsertLog(1, "TCP port " + port + " is open");
            }
            else
            {
                InsertLog(3, "TCP port " + port + " is closed");
            }
        }
    }
}

我知道线程一直在运行,因为我的 CPU 不断获得 35%,但评论t.Start(_ExternalPort);使应用程序正常运行。

此外,我可以确定工作已经完成,因为我可以在我的数据库中检查结果。

t网页的结果处理完毕(使用方法添加到数据库中)后,线程不应该停止并处理InsertLog吗?

4

0 回答 0