每当我的客户端失去与服务器的连接时,我都会有一个重新连接循环,它会不断地寻找服务器。
当这个循环运行时,它每次尝试连接时都会生成一个 conhost.exe 和 csc.exe 进程,直到计算机减速停止。
有谁知道什么会创建这些流程?
所以会发生什么,无论何时出现连接失败或连接丢失,我都会调用 Initialize。这应该正确处理所有组件,然后重新初始化它们。
NetworkInterface 和 TcpInterface 的初始化方法:
public void Initialize()
{
if (ni != null)
{
ni.Dispose();
GC.Collect();
}
if (tcpInterface != null)
{
tcpInterface.Dispose();
}
tcpInterface = new TcpInterface();
if (!string.IsNullOrEmpty(ipAddress))
{
tcpInterface.Settings = new TcpSettings
{
RemoteIp = ipAddress,
Port = _port,
PacketDenotesLength = false
};
}
tcpInterface.NewConnection += new TcpInterface.TcpNetworkStateEventHandler(tcpInterface_NewConnection);
tcpInterface.FailConnection += new TcpInterface.ConnectionEventHandler(tcpInterface_FailConnection);
tcpInterface.ReceivePacket += new TcpInterface.TcpInterfacePacketEventHandler(tcpInterface_ReceivePacket);
tcpInterface.LoseConnection += new TcpInterface.TcpNetworkStateEventHandler(tcpInterface_LoseConnection);
ni = new NetworkInterface<string, PacketInfo>();
ni.Services.Register("TcpInterface", tcpInterface);
ni.Initialize();
}
为 TcpInterface 配置:
public void Dispose()
{
if (TcpClient != null)// && TcpClient.Connected)
{
if (TcpClient.Connected)
{
NetworkStream stream = TcpClient.GetStream();
if (stream != null)
{
stream.Close();
}
}
TcpClient.Close();
TcpClient = null;
}
Buffer = null;
BufferBuilder = null;
}
为 ni 处理:
public void Dispose()
{
Services.Dispose();
}