void UpdateGrid(bool updateDevices) { lock (updateGridLock) { if (updateDevices) { devices = DeviceDetector.GetDevices().ToList(); interfaces = DeviceDetector.GetInterfaces().ToList(); devices.AddRange(interfaces); } var filter = FilterTextBox.Text.Trim(); var view = devices; if (EnableFilterCheckBox.Checked && !string.IsNullOrEmpty(filter)) { view = devices.Where(x => comp(x.ClassDescription, filter) || comp(x.Description, filter) || comp(x.Manufacturer, filter) || comp(x.DeviceId, filter)) .ToList(); } // WORKAROUND: Remove SelectionChanged event. DeviceDataGridView.SelectionChanged -= DeviceDataGridView_SelectionChanged; DeviceDataGridView.DataSource = view; // WORKAROUND: Use BeginInvoke to prevent SelectionChanged firing multiple times. ControlsHelper.BeginInvoke(() => { DeviceDataGridView.SelectionChanged += DeviceDataGridView_SelectionChanged; DeviceDataGridView_SelectionChanged(DeviceDataGridView, new EventArgs()); }); DeviceTabPage.Text = string.Format("{0} Devices on {1:yyyy-MM-dd HH:mm:ss}", view.Count, DateTime.Now); var dis = devices.Where(x => string.IsNullOrEmpty(x.ParentDeviceId)).ToArray(); var classes = devices.Select(x => x.ClassGuid).Distinct(); // Suppress repainting the TreeView until all the objects have been created. DevicesTreeView.Nodes.Clear(); TreeImageList.Images.Clear(); foreach (var cl in classes) { var icon = DeviceDetector.GetClassIcon(cl); if (icon != null) { Image img = new Icon(icon, 16, 16).ToBitmap(); TreeImageList.Images.Add(cl.ToString(), img); } } DevicesTreeView.BeginUpdate(); foreach (DeviceInfo di in dis) { var tn = new TreeNode(System.Environment.MachineName); tn.Tag = di; tn.ImageKey = di.ClassGuid.ToString(); tn.SelectedImageKey = di.ClassGuid.ToString(); DevicesTreeView.Nodes.Add(tn); AddChildren(tn); } DevicesTreeView.EndUpdate(); DevicesTreeView.ExpandAll(); } }
private void UpdateListAndTree(bool updateDevices) { lock (updateGridLock) { if (updateDevices) { var newDevices = DeviceDetector.GetDevices().ToList(); var newInterfaces = DeviceDetector.GetInterfaces().ToList(); if (devices.Count > 0) { var addedDevices = newDevices.Where(n => !devices.Any(o => o.DeviceId == n.DeviceId)).ToList(); var removedDevices = devices.Where(n => !newDevices.Any(o => o.DeviceId == n.DeviceId)).ToList(); AddLog("Added", addedDevices); AddLog("Removed", removedDevices); } if (interfaces.Count > 0) { var addedInterfaces = newInterfaces.Where(n => !interfaces.Any(o => o.DeviceId == n.DeviceId)).ToList(); var removedInterfaces = interfaces.Where(n => !newInterfaces.Any(o => o.DeviceId == n.DeviceId)).ToList(); AddLog("Added", addedInterfaces); AddLog("Removed", removedInterfaces); } // Store new list. devices = newDevices; interfaces = newInterfaces; // Note: 'devices' and 'interfaces' share same DeviceId. // Don't just select by DeviceID from 'devices'. allDevices = newDevices; allDevices.AddRange(newInterfaces); } var filter = FilterStripTextBox.Text.Trim(); var filtered = JocysCom.ClassLibrary.Data.Linq.ApplySearch(allDevices, filter, (x) => { return(string.Join(" ", x.ClassDescription, x.Description, x.Manufacturer, x.DeviceId)); }).ToList(); BindDeviceList(filtered); BindDeviceTree(filtered); } }
private async Task CheckAndClean(bool clean) { LogTextBox.Clear(); MainTabControl.SelectedTab = LogsTabPage; var cancellationToken = new CancellationToken(false); var so = ControlsHelper.MainTaskScheduler; var unused = Task.Factory.StartNew(() => { AddLogLine("Enumerating Devices..."); var devices = DeviceDetector.GetDevices(); var offline = devices.Where(x => !x.IsPresent && x.IsRemovable && !x.Description.Contains("RAS Async Adapter")).ToArray(); var problem = devices.Where(x => x.Status.HasFlag(DeviceNodeStatus.DN_HAS_PROBLEM)).Except(offline).ToArray(); var unknown = devices.Where(x => x.Description.Contains("Unknown")).Except(offline).Except(problem).ToArray(); var list = new List <string>(); if (offline.Length > 0) { list.Add(string.Format("{0} offline devices.", offline.Length)); } if (problem.Length > 0) { list.Add(string.Format("{0} problem devices.", problem.Length)); } if (unknown.Length > 0) { list.Add(string.Format("{0} unknown devices.", unknown.Length)); } var message = string.Join("\r\n", list); if (list.Count == 0) { AddLogLine("No offline, problem or unknown devices found."); } else if (clean) { foreach (var item in list) { AddLogLine(item); } var result = DialogResult.No; ControlsHelper.Invoke(new Action(() => { var form = new JocysCom.ClassLibrary.Controls.MessageBoxForm { StartPosition = FormStartPosition.CenterParent }; ControlsHelper.CheckTopMost(form); result = form.ShowForm( "Do you want to remove offline, problem or unknown devices?\r\n\r\n" + message, "Do you want to remove devices?", MessageBoxButtons.YesNo, MessageBoxIcon.Warning); form.Dispose(); })); if (result != DialogResult.Yes) { return; } var devList = new List <DeviceInfo>(); devList.AddRange(offline); devList.AddRange(problem); devList.AddRange(unknown); for (var i = 0; i < devList.Count; i++) { var item = devList[i]; AddLogLine("Removing Device: {0}/{1} - {2}", i + 1, list.Count, item.Description); try { var exception = DeviceDetector.RemoveDevice(item.DeviceId); if (exception != null) { AddLogLine(exception.Message); } //System.Windows.Forms.Application.DoEvents(); } catch (Exception ex) { AddLogLine(ex.Message); } } } AddLogLine("Done"); }, CancellationToken.None, TaskCreationOptions.LongRunning, so).ConfigureAwait(true); }