private void BeginRestoreApplicationsOnCurrentDisplays()
        {
            if (this.isRestoring)
            {
                return;
            }
            this.isRestoring = true; // Prevent any accidental re-reading of layout while we attempt to restore layout

            var thread = new Thread(() =>
            {
                try
                {
                    StateDetector.WaitForWindowStabilization(() =>
                    {
                        RestoreApplicationsOnCurrentDisplays();
                    });
                }
                catch (Exception ex)
                {
                    Log.Error(ex.ToString());
                }
                this.isRestoring           = false;
                this.ignoreCaptureRequests = false; // Resume handling of capture requests

                //this.BeginCaptureApplicationsOnCurrentDisplays();
            });

            //thread.IsBackground = true;
            thread.Name = "PersistentWindowProcessor.RestoreApplicationsOnCurrentDisplays()";
            thread.Start();
        }
        private void BeginCaptureApplicationsOnCurrentDisplays()
        {
            if (!this.IsCaptureAllowed())
            {
                Log.Trace("Ignoring capture request... IsCaptureAllowed() returned false");
                return;
            }

            this.isCapturing = true;

            var thread = new Thread(() =>
            {
                StateDetector.WaitForWindowStabilization(() =>
                {
                    CaptureApplicationsOnCurrentDisplays();
                    this.isCapturing = false;

                    //if (this.pendingCaptureRequest)
                    //{
                    //    // If we had more changes during capture, this should be set to true, so we perform a follow-up capture.
                    //    this.pendingCaptureRequest = false;
                    //    this.BeginCaptureApplicationsOnCurrentDisplays();
                    //}
                });
            })
            {
                IsBackground = true,
                Name         = "PersistentWindowProcessor.BeginCaptureApplicationsOnCurrentDisplays()"
            };

            thread.Start();
        }
        /**
         * Create event handlers needed to detect various state changes that affect window capture.
         * This is done explicitly with assigned fields to allow proper disposal. According to the
         * documentation, if not properly disposed, there will be leaks (although I'm skeptical for
         * this use case, since they live the lifetime of the process).
         */
        private void CreateEventHandlers()
        {
            this.displaySettingsChangingHandler = (s, e) =>
            {
                Log.Trace("Display settings changing handler invoked");
                this.ignoreCaptureRequests = true;
                CancelDelayedCapture(); // Throw away any pending captures
            };

            this.displaySettingsChangedHandler = (s, e) =>
            {
                Log.Trace("Display settings changed");
                StateDetector.WaitForWindowStabilization(() =>
                {
                    // CancelDelayedCapture(); // Throw away any pending captures
                    BeginRestoreApplicationsOnCurrentDisplays();
                });
            };

            this.powerModeChangedHandler = (s, e) =>
            {
                switch (e.Mode)
                {
                case PowerModes.Suspend:
                    Log.Info("System Suspending");
                    break;

                case PowerModes.Resume:
                    Log.Info("System Resuming");
                    this.ignoreCaptureRequests = true;
                    CancelDelayedCapture();     // Throw away any pending captures
                    BeginRestoreApplicationsOnCurrentDisplays();
                    break;

                default:
                    Log.Trace("Unhandled power mode change: {0}", nameof(e.Mode));
                    break;
                }
            };

            this.sessionSwitchEventHandler = (sender, args) =>
            {
                if (args.Reason == SessionSwitchReason.SessionLock)
                {
                    Log.Trace("Session locked");
                    this.isSessionLocked = true;
                }
                else if (args.Reason == SessionSwitchReason.SessionUnlock)
                {
                    Log.Trace("Session unlocked");
                    this.isSessionLocked = false;
                }
            };
        }