@@ -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