private void Scan() { // get a list of all current devices attached to the computer // Go through the list of existing Boards and remove any that no longer exist. List<TreehopperBoard> BoardsToRemove = new List<TreehopperBoard>(); foreach(var board in Boards.ToList()) { bool boardExistsInDeviceList = false; foreach (UsbRegistry regDevice in UsbDevice.AllDevices) { if (regDevice.Vid == vid && regDevice.Pid == pid) { if(regDevice.Device != null) { TreehopperBoard newBoard = new TreehopperBoard(regDevice.Device); if (newBoard.Equals(board)) { boardExistsInDeviceList = true; } } else { // If this property reads null, it's probably because the board is open. boardExistsInDeviceList = true; } } } if (!boardExistsInDeviceList) { BoardsToRemove.Add(board); } } foreach(var board in BoardsToRemove) { Boards.Remove(board); Debug.WriteLine("New board list has " + Boards.Count + " Boards"); } // Now add any new boards foreach (UsbRegistry regDevice in UsbDevice.AllDevices) { if (regDevice.Vid == vid && regDevice.Pid == pid) { if(regDevice.Device != null) { TreehopperBoard newBoard = new TreehopperBoard(regDevice.Device); if (PassesFilter(newBoard)) { // add the board to the list if it doesn't already exist bool alreadyInList = false; foreach (var board in Boards) { if (board.Equals(newBoard)) { alreadyInList = true; } } if (!alreadyInList) { Boards.Add(newBoard); Debug.WriteLine("Adding " + newBoard.ToString() + ". New board list has " + Boards.Count + " Boards"); } } } } } }