/// <summary> /// Event handler for polling timer. Look for new devices and remove old ones. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private static void FindDeviceTimer_Elapsed(object sender, ElapsedEventArgs e) { TimeSinceReCheck += QueryInterval; if (TimeSinceReCheck - ReCheckInterval > 0) { TimeSinceReCheck = 0; var enumerator = DeviceList.GetEnumerator(); while (enumerator.MoveNext()) { var key = enumerator.Current.Key; try { // If it is time to recheck devices, mark every device as not discovered if (!DeviceList.TryUpdate(key, false, true)) { // TryUpdate returned false. // The device has already was not found the previous recheck cycle. Assume it is gone and notify the user. bool ret; if (DeviceList.TryRemove(key, out ret)) { PrintQueue.Enqueue($"Lost: {key}"); } } } catch (KeyNotFoundException) { } // Device was already removed. } } // Look for new and previously discovered devices MDns.SendQuery(ServiceToQuery); }