private void captureApplicationsOnCurrentDisplays(bool initialCapture = false)
        {
            lock (_displayChangeLock)
            {
                var desktopKey = _desktopService.GetDesktopKey();

                _logger?.LogInformation($"Capture applications for desktop '{desktopKey}' started.");

                try
                {
                    if (!_desktopApplications.TryGetValue(desktopKey, out var applications))
                    {
                        _logger.LogInformation($"New desktop with DesktopKey '{desktopKey}' has been identified.");
                        applications = new SortedDictionary <string, ApplicationDisplayMetrics>();
                        _desktopApplications.Add(desktopKey, applications);
                    }

                    var updateLogs = new List <string>();
                    var updateApps = new List <ApplicationDisplayMetrics>();
                    var appWindows = _windowService.CaptureWindowsOfInterest();

                    foreach (var window in appWindows)
                    {
                        if (hasWindowChanged(applications, window, out ApplicationDisplayMetrics curDisplayMetrics))
                        {
                            updateApps.Add(curDisplayMetrics);
                            _logger.LogTrace($"[{window.Title}][{(window.Visible ? "V" : "H")}] {curDisplayMetrics.ToString()}");
                        }
                    }

                    for (var i = 0; i < updateApps.Count; i++)
                    {
                        var curDisplayMetrics = updateApps[i];

                        if (applications.TryGetValue(curDisplayMetrics.Key, out var appMetrics))
                        {
                            appMetrics.WindowPlacement = curDisplayMetrics.WindowPlacement;
                            appMetrics.ScreenPosition  = curDisplayMetrics.ScreenPosition;
                        }
                        else
                        {
                            applications.Add(curDisplayMetrics.Key, curDisplayMetrics);
                        }
                    }

                    _logger?.LogInformation($"Capture applications for desktop '{desktopKey}' completed.");
                }
                catch (Exception ex)
                {
                    _logger?.LogError(ex, $"Capture applications for desktop '{desktopKey}' failed.");
                }
            }
        }
Ejemplo n.º 2
0
        public void WaitForWindowStabilization(Action completeCallback, int additionalDelayInMs = 0)
        {
            var previousLocations = new Dictionary <IntPtr, RECT>();
            var currentLocations  = new Dictionary <IntPtr, RECT>();

            if (previousLocations.Count == 0)
            {
                var windows = _windowService.CaptureWindowsOfInterest();
                getWindowLocations(previousLocations, windows);
            }

            while (true)
            {
                _logger?.LogTrace("Windows not stable, waiting...");
                //await Delay(100);
                Thread.Sleep(STABILIZATION_WAIT_INTERVAL);
                var windows = _windowService.CaptureWindowsOfInterest();
                getWindowLocations(currentLocations, windows);

                if (doLocationsMatch(previousLocations, currentLocations))
                {
                    if (additionalDelayInMs > 0)
                    {
                        Thread.Sleep(additionalDelayInMs);
                    }
                    completeCallback();
                    break;
                }

                previousLocations.Clear();
                foreach (var currentLocation in currentLocations)
                {
                    previousLocations[currentLocation.Key] = currentLocation.Value;
                }
                currentLocations.Clear();
            }
        }