private static void EnumerationThreadProc() { var oldDevices = new PIEDevice[0]; while (true) { try { var devices = PIEDevice.EnumeratePIE(); foreach (var pieDevice in devices.Where(d => d.HidUsagePage == 0xC && !oldDevices.Any(od => DeviceEquals(od, d)))) { var device = new XKeysDevice(pieDevice); lock (((IList)Devices).SyncRoot) Devices.Add(device); DeviceConnected?.Invoke(null, device); Logger.Info("New device connected {0}:{1}", pieDevice.Pid, pieDevice.Vid); } foreach (var pieDevice in oldDevices.Where(d => d.HidUsagePage == 0xC && !devices.Any(od => DeviceEquals(od, d)))) { var device = Devices.FirstOrDefault(d => DeviceEquals(d.PieDevice, pieDevice)); if (device == null) { continue; } device.Dispose(); lock (((IList)Devices).SyncRoot) Devices.Remove(device); Logger.Info("Device disconnected {0}:{1}", pieDevice.Pid, pieDevice.Vid); } oldDevices = devices; Thread.Sleep(1000); } catch (Exception e) { Logger.Error(e); } } }