From a2be258212e1b3c41b53c0f4db587c73947e436f Mon Sep 17 00:00:00 2001 From: Michael Higgins Date: Tue, 2 Feb 2021 12:30:37 -0500 Subject: [PATCH] Update Program.cs catch NullReferenceException add delay to the displaysettingschanged event to give the system time to finish making those adjustments --- CenterTaskbar/Program.cs | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/CenterTaskbar/Program.cs b/CenterTaskbar/Program.cs index 9ce0280..9bf93db 100644 --- a/CenterTaskbar/Program.cs +++ b/CenterTaskbar/Program.cs @@ -282,8 +282,9 @@ private void Start() Automation.AddAutomationEventHandler(WindowPattern.WindowClosedEvent, Desktop, TreeScope.Subtree, _uiaEventHandler); } - private void SystemEvents_DisplaySettingsChanged(object sender, EventArgs e) + private async void SystemEvents_DisplaySettingsChanged(object sender, EventArgs e) { + await Task.Delay(3000); Restart(sender, e); } @@ -401,7 +402,19 @@ private bool PositionLoop(AutomationElement trayWnd) } // Right bounds check - var rightBounds = SideBoundary(false, horizontal, taskList, scale, trayBounds); + int rightBounds; + int leftBounds; + try + { + rightBounds = SideBoundary(false, horizontal, taskList, scale, trayBounds); + leftBounds = SideBoundary(true, horizontal, taskList, scale, trayBounds); + } + catch (NullReferenceException) + { + Reset(trayWnd); + return true; + } + if (targetPos + size > rightBounds) { // Shift off center when the bar is too big @@ -411,7 +424,6 @@ private bool PositionLoop(AutomationElement trayWnd) } // Left bounds check - var leftBounds = SideBoundary(true, horizontal, taskList, scale, trayBounds); if (targetPos <= leftBounds) { // Prevent X position ending up beyond the normal left aligned position