public async Task PopulateAsync() { System = GetSystem(); var sw = new Stopwatch(); var tasks = new[] { GetTask(nameof(DeviceItems), () => DeviceItems = DeviceContext.EnumerateMonitorDevices().ToArray()), GetTask(nameof(DisplayMonitorItems), async() => { if (OsVersion.Is10Redstone4OrNewer) { DisplayMonitorItems = await DisplayMonitor.GetDisplayMonitorsAsync(); } }), GetTask(nameof(DisplayConfigItems), () => DisplayConfigItems = DisplayConfig.EnumerateDisplayConfigs().ToArray()), GetTask(nameof(InstalledItems), () => InstalledItems = DeviceInformation.EnumerateInstalledMonitors().ToArray()), GetTask(nameof(PhysicalItems), () => PhysicalItems = DeviceContext.GetMonitorHandles().ToDictionary( x => x, x => MonitorConfiguration.EnumeratePhysicalMonitors(x.MonitorHandle, true) .Select(x => new PhysicalItemPlus(x)) .ToArray())), GetTask(nameof(DesktopItems), () => DesktopItems = MSMonitor.EnumerateDesktopMonitors().ToArray()) }; sw.Start(); ElapsedTime = await Task.WhenAll(tasks); sw.Stop(); Task <string> GetTask(string name, Action action) => Task.Run(() => { action.Invoke(); var elapsed = sw.Elapsed; return($@"{name,-14} -> {elapsed.ToString($@"{(elapsed.Minutes > 0 ? @"m\:" : string.Empty)}s\.fff")}"); }); }
public async Task PopulateAsync() { var sw = new Stopwatch(); var actions = new[] { GetAction(nameof(DeviceItems), () => DeviceItems = DeviceContext.EnumerateMonitorDevices().ToArray()), GetAction(nameof(PhysicalItems), () => PhysicalItems = DeviceContext.GetMonitorHandles().ToDictionary( x => x, x => MonitorConfiguration.EnumeratePhysicalMonitors(x.MonitorHandle, true).ToArray())), GetAction(nameof(InstalledItems), () => InstalledItems = DeviceInstallation.EnumerateInstalledMonitors().ToArray()), GetAction(nameof(DesktopItems), () => DesktopItems = MSMonitor.EnumerateDesktopMonitors().ToArray()), GetAction(nameof(DisplayItems), async() => { if (OsVersion.Is10Redstone4OrNewer) { DisplayItems = await DisplayInformation.GetDisplayMonitorsAsync(); } }) }; ElapsedTime = new string[actions.Length]; sw.Start(); await Task.WhenAll(actions.Select((x, index) => Task.Run(() => x.Invoke(index)))); sw.Stop(); Action <int> GetAction(string name, Action action) => new Action <int>((index) => { action.Invoke(); ElapsedTime[index] = $"{name} -> {sw.ElapsedMilliseconds}"; }); }
private static IEnumerable <IMonitor> EnumerateMonitors(List <DeviceItemPlus> deviceItems) { if (!(deviceItems?.Any() == true)) { yield break; } // Obtained by DDC/CI foreach (var handleItem in DeviceContext.GetMonitorHandles()) { foreach (var physicalItem in MonitorConfiguration.EnumeratePhysicalMonitors(handleItem.MonitorHandle)) { int index = -1; if (physicalItem.IsSupported) { index = deviceItems.FindIndex(x => (x.DisplayIndex == handleItem.DisplayIndex) && (x.MonitorIndex == physicalItem.MonitorIndex) && string.Equals(x.Description, physicalItem.Description, StringComparison.OrdinalIgnoreCase)); } if (index < 0) { physicalItem.Handle.Dispose(); continue; } var deviceItem = deviceItems[index]; yield return(new DdcMonitorItem( deviceInstanceId: deviceItem.DeviceInstanceId, description: deviceItem.AlternateDescription, displayIndex: deviceItem.DisplayIndex, monitorIndex: deviceItem.MonitorIndex, handle: physicalItem.Handle, useLowLevel: physicalItem.IsLowLevelSupported)); deviceItems.RemoveAt(index); if (deviceItems.Count == 0) { yield break; } } } // Obtained by WMI var installedItems = DeviceInstallation.EnumerateInstalledMonitors().ToArray(); foreach (var desktopItem in MSMonitor.EnumerateDesktopMonitors()) { foreach (var installedItem in installedItems) { int index = -1; if (desktopItem.BrightnessLevels.Any()) { index = deviceItems.FindIndex(x => string.Equals(x.DeviceInstanceId, desktopItem.DeviceInstanceId, StringComparison.OrdinalIgnoreCase) && string.Equals(x.DeviceInstanceId, installedItem.DeviceInstanceId, StringComparison.OrdinalIgnoreCase)); } if (index < 0) { continue; } var deviceItem = deviceItems[index]; yield return(new WmiMonitorItem( deviceInstanceId: deviceItem.DeviceInstanceId, description: deviceItem.AlternateDescription, displayIndex: deviceItem.DisplayIndex, monitorIndex: deviceItem.MonitorIndex, brightnessLevels: desktopItem.BrightnessLevels, isRemovable: installedItem.IsRemovable)); deviceItems.RemoveAt(index); if (deviceItems.Count == 0) { yield break; } } } // Unreachable neither by DDC/CI nor by WMI foreach (var deviceItem in deviceItems) { yield return(new UnreachableMonitorItem( deviceInstanceId: deviceItem.DeviceInstanceId, description: deviceItem.AlternateDescription, displayIndex: deviceItem.DisplayIndex, monitorIndex: deviceItem.MonitorIndex, isInternal: deviceItem.IsInternal)); } }