@@ -49,9 +49,6 @@ private void SetDataContexts()
4949 // Set DataContext for the association view
5050 this . AssociationView . DataContext = this . associationViewModel ;
5151
52- // Set DataContext for the performance view
53- this . PerformanceViewControl . DataContext = this . performanceViewModel ;
54-
5552 // Set DataContext for the log viewer view
5653 this . LogViewerViewControl . DataContext = this . logViewerViewModel ;
5754
@@ -1001,57 +998,7 @@ private async Task UpdateSystemTrayContextMenuAsync()
1001998 {
1002999 try
10031000 {
1004- // Update power plans in system tray
1005- var powerPlanService = this . serviceProvider . GetRequiredService < IPowerPlanService > ( ) ;
1006- var powerPlans = await powerPlanService . GetPowerPlansAsync ( ) ;
1007- var activePowerPlan = await powerPlanService . GetActivePowerPlan ( ) ;
1008- this . systemTrayService . UpdatePowerPlans ( powerPlans , activePowerPlan ) ;
1009-
1010- // Update profiles in system tray
1011- var profilesDirectory = StoragePaths . ProfilesDirectory ;
1012- var profileNames = new List < string > ( ) ;
1013-
1014- if ( Directory . Exists ( profilesDirectory ) )
1015- {
1016- profileNames = Directory . GetFiles ( profilesDirectory , "*.json" )
1017- . Select ( Path . GetFileNameWithoutExtension )
1018- . Where ( name => ! string . IsNullOrWhiteSpace ( name ) )
1019- . ToList ( ) ! ;
1020- }
1021-
1022- this . systemTrayService . UpdateProfiles ( profileNames ) ;
1023-
1024- // Update system status (with timeout to prevent hanging)
1025- try
1026- {
1027- var performanceService = this . serviceProvider . GetRequiredService < IPerformanceMonitoringService > ( ) ;
1028- var metricsTask = performanceService . GetSystemMetricsAsync ( lightweight : true ) ;
1029- var metricsResult = await Task . WhenAny ( metricsTask , Task . Delay ( 2000 ) ) ; // 2 second timeout
1030-
1031- if ( metricsResult == metricsTask )
1032- {
1033- var currentMetrics = await metricsTask ;
1034- this . systemTrayService . UpdateSystemStatus (
1035- activePowerPlan ? . Name ?? "Unknown" ,
1036- currentMetrics ? . TotalCpuUsage ?? 0.0 ,
1037- currentMetrics ? . MemoryUsagePercentage ?? 0.0 ) ;
1038- }
1039- else
1040- {
1041- // Timeout - use default values
1042- this . systemTrayService . UpdateSystemStatus (
1043- activePowerPlan ? . Name ?? "Unknown" ,
1044- 0.0 , 0.0 ) ;
1045- }
1046- }
1047- catch ( Exception metricsEx )
1048- {
1049- System . Diagnostics . Debug . WriteLine ( $ "Failed to get performance metrics for tray: { metricsEx . Message } ") ;
1050- // Use default values
1051- this . systemTrayService . UpdateSystemStatus (
1052- activePowerPlan ? . Name ?? "Unknown" ,
1053- 0.0 , 0.0 ) ;
1054- }
1001+ await this . systemTrayStatusUpdater . UpdateContextMenuAsync ( this . systemTrayService ) ;
10551002 }
10561003 catch ( Exception ex )
10571004 {
@@ -1065,6 +1012,12 @@ private void StartSystemTrayUpdateTimer()
10651012 {
10661013 this . systemTrayUpdateTimer ? . Stop ( ) ;
10671014 this . systemTrayUpdateTimer ? . Dispose ( ) ;
1015+ this . systemTrayUpdateTimer = null ;
1016+
1017+ if ( ! this . systemTrayStatusUpdater . ShouldRunPerformanceStatusUpdates )
1018+ {
1019+ return ;
1020+ }
10681021
10691022 this . systemTrayUpdateFailureStreak = 0 ;
10701023 this . systemTrayUpdateTimer = new System . Timers . Timer ( SystemTrayUpdateBaseIntervalMs ) ;
@@ -1142,21 +1095,9 @@ private async Task<bool> UpdateSystemTrayStatusAsync()
11421095 {
11431096 try
11441097 {
1145- var powerPlanService = this . serviceProvider . GetRequiredService < IPowerPlanService > ( ) ;
1146- var performanceService = this . serviceProvider . GetRequiredService < IPerformanceMonitoringService > ( ) ;
1147-
1148- var activePowerPlan = await powerPlanService . GetActivePowerPlan ( ) ;
1149- var currentMetrics = await performanceService . GetSystemMetricsAsync ( lightweight : true ) ;
1150-
1151- await this . Dispatcher . InvokeAsync ( ( ) =>
1152- {
1153- this . systemTrayService . UpdateSystemStatus (
1154- activePowerPlan ? . Name ?? "Unknown" ,
1155- currentMetrics ? . TotalCpuUsage ?? 0.0 ,
1156- currentMetrics ? . MemoryUsagePercentage ?? 0.0 ) ;
1157- } ) ;
1158-
1159- return true ;
1098+ return await this . systemTrayStatusUpdater . UpdateStatusAsync (
1099+ this . systemTrayService ,
1100+ action => this . Dispatcher . InvokeAsync ( action ) . Task ) ;
11601101 }
11611102 catch ( Exception ex )
11621103 {
@@ -1335,6 +1276,12 @@ private void ResumeForegroundRefreshes()
13351276 this . isSystemTrayUpdatesSuspended = false ;
13361277 this . systemTrayUpdateFailureStreak = 0 ;
13371278 this . systemTrayUpdateTimer ? . Stop ( ) ;
1279+
1280+ if ( ! this . systemTrayStatusUpdater . ShouldRunPerformanceStatusUpdates )
1281+ {
1282+ return ;
1283+ }
1284+
13381285 if ( this . systemTrayUpdateTimer != null )
13391286 {
13401287 this . systemTrayUpdateTimer . Interval = SystemTrayUpdateBaseIntervalMs ;
@@ -1405,9 +1352,9 @@ private void ApplyAppRefreshPolicy(AppActivityState state)
14051352
14061353 if ( decision . PerformanceUiMonitoringEnabled )
14071354 {
1408- _ = this . performanceViewModel . ActivateDiagnosticsAsync ( ) ;
1355+ _ = this . GetPerformanceViewModel ( ) . ActivateDiagnosticsAsync ( ) ;
14091356 }
1410- else
1357+ else if ( this . performanceViewModel != null )
14111358 {
14121359 _ = this . performanceViewModel . SuspendBackgroundMonitoringAsync ( ) ;
14131360 }
@@ -1538,6 +1485,11 @@ private void SelectMainTab(string tag)
15381485 return ;
15391486 }
15401487
1488+ if ( string . Equals ( tag , "Performance" , StringComparison . Ordinal ) )
1489+ {
1490+ this . GetPerformanceViewModel ( ) ;
1491+ }
1492+
15411493 this . ApplySectionVisibility ( tag ) ;
15421494
15431495 if ( string . Equals ( tag , "Performance" , StringComparison . Ordinal ) )
@@ -1842,6 +1794,11 @@ private async Task NavMenuItem_ClickAsync(object sender, RoutedEventArgs e)
18421794 return ;
18431795 }
18441796
1797+ if ( string . Equals ( tag , "Performance" , StringComparison . Ordinal ) )
1798+ {
1799+ this . GetPerformanceViewModel ( ) ;
1800+ }
1801+
18451802 this . ApplySectionVisibility ( tag ) ;
18461803
18471804 if ( string . Equals ( tag , "Performance" , StringComparison . Ordinal ) )
@@ -1897,6 +1854,7 @@ protected override void OnClosed(EventArgs e)
18971854
18981855 this . initializationTimeoutTimer ? . Stop ( ) ;
18991856 this . initializationTimeoutTimer ? . Dispose ( ) ;
1857+ this . performanceViewModel ? . Dispose ( ) ;
19001858
19011859 this . selfResourceManagementService . RestoreForegroundMode ( ) ;
19021860 this . navigationBehavior . Dispose ( ) ;
0 commit comments