我有以下启动服务器的方法:
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
?