C# 列出自动处理的对象#

C# 列出自动处理的对象#,c#,C#,我有一个包含自定义对象的公共列表,我已将其声明为windows窗体 private List<ConnectedClient> connectedClients = new List<ConnectedClient>(); 当客户端提交消息时,我根据客户端对象接收到的数据过滤事件服务器上的ConnectedClientSubmitsm列表,然后决定接受还是拒绝来自客户端的数据 private void server_evClientSubmitSm(object sen

我有一个包含自定义对象的公共列表,我已将其声明为windows窗体

private List<ConnectedClient> connectedClients = new List<ConnectedClient>();
当客户端提交消息时,我根据客户端对象接收到的数据过滤事件服务器上的ConnectedClientSubmitsm列表,然后决定接受还是拒绝来自客户端的数据

private void server_evClientSubmitSm(object sender, ServerClient client, SubmitSm data)
    {
        try
        {                
            ConnectedClient clientDto = null;
            lock (connectedClients)
            {
                clientDto = connectedClients.Where(c => !string.IsNullOrWhiteSpace(c.ServerClient.SystemID) && c.ServerClient.SystemID.Trim() == client.SystemID.Trim()
                && c.ServerClient.LocalEndPoint.Address != null && c.ServerClient.LocalEndPoint.Address == client.LocalEndPoint.Address
                && c.ServerClient.LocalEndPoint.Port > new int() && c.ServerClient.LocalEndPoint.Port == client.LocalEndPoint.Port).FirstOrDefault();
            }

            if (clientDto != null)
            {
                //receive Message
            }
            else
            {
                //object not exist 
            }
        }
    }
但在应用程序休眠或暂停(客户端不提交消息)一段时间后,他返回提交消息,但下面的部分返回null

ConnectedClient clientDto = null;
        lock (connectedClients)
        {
            clientDto = connectedClients.Where(c => !string.IsNullOrWhiteSpace(c.ServerClient.SystemID) && c.ServerClient.SystemID.Trim() == client.SystemID.Trim()
            && c.ServerClient.LocalEndPoint.Address != null && c.ServerClient.LocalEndPoint.Address == client.LocalEndPoint.Address
            && c.ServerClient.LocalEndPoint.Port > new int() && c.ServerClient.LocalEndPoint.Port == client.LocalEndPoint.Port).FirstOrDefault();
        }

我想问题是在几分钟后内存释放了列表对象,有人可以建议吗?

当您的机器进入睡眠状态时,所有连接都会关闭

如果您等待很长时间而什么也不做,您的连接可能也会因为超时而关闭

因此,您的列表不是空的,您的问题与已处理的对象无关。您的列表仍然已满,但它包含表示现在已关闭的连接的
ConnectedClient
对象,因此其属性(如
c.ServerClient
c.ServerClient.LocalEndPoint
)无效,因此
Where()
无法找到匹配项

.FirstOrDefault()
call将返回找到的第一个项目,如果没有找到项目,则返回
null


由于没有找到任何项目,您将得到
null

问题出在哪里?发生了什么您不希望发生的事情?你认为哪些事情不会发生?你的意思是你观察到列表是空的,而不是被处理掉的?只要表单没有关闭,运行时就会保持它不变,除非您干预。顺便说一句,因为您使用的是锁,所以您可能以某种方式使用了线程,如果操作不当,可能会把事情搞得一团糟,比如锁定。提示:尝试使用并发集合:。这个收藏还在哪里使用?你能把代码也发出来吗?他把它描述为应用程序睡眠,不确定这就是他所说的机器。@PeterBons我不知道什么是“应用程序睡眠”。但无论如何,我也提到了超时。
ConnectedClient clientDto = null;
        lock (connectedClients)
        {
            clientDto = connectedClients.Where(c => !string.IsNullOrWhiteSpace(c.ServerClient.SystemID) && c.ServerClient.SystemID.Trim() == client.SystemID.Trim()
            && c.ServerClient.LocalEndPoint.Address != null && c.ServerClient.LocalEndPoint.Address == client.LocalEndPoint.Address
            && c.ServerClient.LocalEndPoint.Port > new int() && c.ServerClient.LocalEndPoint.Port == client.LocalEndPoint.Port).FirstOrDefault();
        }