public void DaemonWork(object state) { while (true) { for (int i = 0; i < UserTokens.Count; i++) { try { if ((DateTime.Now - UserTokens[i].ActiveDateTime).Milliseconds > TCPServer.SocketTimeOutMS) { lock (UserTokens[i]) { App.Error("连接超时,即将关闭连接TokenID={0}", UserTokens[i].TokenID); TCPServer.CloseSocket(UserTokens[i]); } } } catch (Exception e) { App.Error("守护线程检测到Socket通信异常, 异常消息: {0}", e.Message); App.Error(e.StackTrace); } } Thread.Sleep(10); // 每10毫秒检测一次 } }