Skip to content

Commit 980e9bd

Browse files
Merge pull request #841 from erikdarlingdata/fix/dashboard-unloaded-cleanup
Fix legend duplication on tab switch
2 parents 58368c6 + 1e40ef3 commit 980e9bd

2 files changed

Lines changed: 20 additions & 4 deletions

File tree

Dashboard/MainWindow.xaml.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -886,6 +886,7 @@ private void CloseTab_Click(object sender, RoutedEventArgs e)
886886
serverTab.AlertAcknowledged -= handler;
887887
_alertAcknowledgedHandlers.Remove(tabId);
888888
}
889+
serverTab.CleanupOnClose();
889890
}
890891
_openTabs.Remove(tabId);
891892
_tabBadges.Remove(tabId);
@@ -1092,6 +1093,7 @@ private async void RemoveServer_Click(object sender, RoutedEventArgs e)
10921093

10931094
if (_openTabs.TryGetValue(server.Id, out var tabItem))
10941095
{
1096+
if (tabItem.Content is ServerTab st) st.CleanupOnClose();
10951097
_openTabs.Remove(server.Id);
10961098
ServerTabControl.Items.Remove(tabItem);
10971099
}

Dashboard/ServerTab.xaml.cs

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,8 @@ private async void StartAutoRefreshLoop(int intervalSeconds)
377377
if (cts.Token.IsCancellationRequested) break;
378378
if (_isRefreshing) continue;
379379

380+
_isRefreshing = true;
381+
_refreshStartedUtc = DateTime.UtcNow;
380382
try
381383
{
382384
var sw = System.Diagnostics.Stopwatch.StartNew();
@@ -387,14 +389,17 @@ private async void StartAutoRefreshLoop(int intervalSeconds)
387389
}
388390
catch (OperationCanceledException) when (!cts.Token.IsCancellationRequested)
389391
{
390-
// SQL query cancelled or timed out, but our loop CTS is still alive — keep going
391392
Logger.Error($"Auto-refresh query cancelled for {_serverConnection.DisplayName}, continuing loop");
392393
}
393394
catch (Exception ex) when (ex is not OperationCanceledException)
394395
{
395396
Logger.Error($"Auto-refresh error: {ex.Message}", ex);
396397
StatusText.Text = "Auto-refresh error";
397398
}
399+
finally
400+
{
401+
_isRefreshing = false;
402+
}
398403
}
399404
}
400405
catch (OperationCanceledException)
@@ -405,9 +410,18 @@ private async void StartAutoRefreshLoop(int intervalSeconds)
405410

406411
private void ServerTab_Unloaded(object sender, RoutedEventArgs e)
407412
{
408-
// Don't cancel auto-refresh on tab switch — WPF fires Unloaded when
409-
// a TabItem is deselected, not just when the control is destroyed.
410-
// The loop is lightweight and should keep ticking in the background.
413+
// WPF fires Unloaded on tab switch, not just destruction.
414+
// Don't tear down state here — the auto-refresh loop and chart
415+
// state must survive tab switches. Cleanup happens when the tab
416+
// is actually removed from the TabControl (via CleanupOnClose).
417+
}
418+
419+
/// <summary>
420+
/// Full cleanup — call when the server tab is permanently removed, not on tab switch.
421+
/// </summary>
422+
public void CleanupOnClose()
423+
{
424+
_autoRefreshCts?.Cancel();
411425
_autoRefreshTimer?.Stop();
412426
_autoRefreshTimer = null;
413427

0 commit comments

Comments
 (0)