private void KeepAliveThread() { var sleepPeriod = new TimeSpan(0, 0, MqttMsgConnect.KEEP_ALIVE_PERIOD_DEFAULT); while (this.IsRunning) { try { var now = Environment.TickCount; foreach (var clientConnection in MqttBroker.GetAllConnectedClients()) { if (clientConnection.IsRunning && clientConnection.IsConnected) { var delta = now - clientConnection.LastCommunicationTime; if (delta >= clientConnection.KeepAlivePeriod) { clientConnection.OnConnectionClosed(); } } } Thread.Sleep(sleepPeriod); } catch (Exception) { } } }