Example #1
0
            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")}");
                });
            }
Example #2
0
        private static async Task <List <DeviceItemPlus> > GetMonitorDevicesAsync()
        {
            IDisplayItem[] displayItems = OsVersion.Is10Redstone4OrNewer
                                ? await DisplayMonitor.GetDisplayMonitorsAsync()
                                : DisplayConfig.EnumerateDisplayConfigs().ToArray();

            var deviceItems = DeviceContext.EnumerateMonitorDevices().ToArray();

            _ids = new HashSet <string>(deviceItems.Select(x => x.DeviceInstanceId));

            IEnumerable <DeviceItemPlus> Enumerate()
            {
                foreach (var deviceItem in deviceItems)
                {
                    var displayItem = displayItems.FirstOrDefault(x => string.Equals(deviceItem.DeviceInstanceId, x.DeviceInstanceId, StringComparison.OrdinalIgnoreCase));
                    if (displayItem is not null)
                    {
                        var isDescriptionNullOrWhiteSpace = string.IsNullOrWhiteSpace(deviceItem.Description);
                        if (isDescriptionNullOrWhiteSpace ||
                            Regex.IsMatch(deviceItem.Description, "^Generic (?:PnP|Non-PnP) Monitor$", RegexOptions.IgnoreCase))
                        {
                            if (!string.IsNullOrWhiteSpace(displayItem.DisplayName))
                            {
                                yield return(new DeviceItemPlus(deviceItem, displayItem.DisplayName, displayItem.IsInternal));

                                continue;
                            }
                            if (!isDescriptionNullOrWhiteSpace &&
                                !string.IsNullOrWhiteSpace(displayItem.ConnectionDescription))
                            {
                                yield return(new DeviceItemPlus(deviceItem, $"{deviceItem.Description} ({displayItem.ConnectionDescription})", displayItem.IsInternal));

                                continue;
                            }
                        }
                    }
                    yield return(new DeviceItemPlus(deviceItem));
                }
            }

            return(Enumerate().Where(x => !string.IsNullOrWhiteSpace(x.AlternateDescription)).ToList());
        }