C# 螺纹不';工作结束后不要自行终止
我使用以下方法启动服务器:C# 螺纹不';工作结束后不要自行终止,c#,multithreading,load,C#,Multithreading,Load,我使用以下方法启动服务器: public void EngageServer() { this.tcpListener = new TcpListener(IPAddress.Any, _InternalPort); this.listenThread = new Thread(RunServer); this.listenThread.Start(); Thread t = new Thread(new Para
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)代码>使应用程序正常运行
此外,我可以肯定工作已经完成,因为我可以检查我的数据库的结果
网页的结果处理后(使用InsertLog
方法添加到数据库中),线程t
是否应该停止并处理?放入一些Debug.WriteLines()并输出一些时间,这样您就可以看到它使用时间的位置。或者,使用VisualStudio的性能工具来计时(如果您有),您似乎没有任何代码循环并保持线程运行。我建议您调试该程序,并使用Visual Studio的“全部中断”功能(IDE中的“暂停”按钮)查看它在异常运行时正在执行的操作。是的,这显然是问题所在。我修改了代码,下载了谷歌的html,应用程序正常运行。