Skip to content

Commit 594a3c4

Browse files
committed
Refactor MainWindow behaviors into partial class
Move and centralize MainWindow behavior/logic into a new partial file and tidy related viewmodel/service code. - Add MainWindow.Behaviors.partial.cs containing initialization, loading overlay, view model loading, service setup, system tray management, monitoring lifecycle, keyboard shortcuts, elevation/performance overlays, graceful shutdown and related helpers (moved out of MainWindow.xaml.cs). - Remove duplicated behavior code from MainWindow.xaml.cs. - Add ProcessViewModel.Behaviors.partial.cs and update ViewModels/ProcessViewModel.cs to separate behavior logic from core viewmodel. - Update Models/ProcessModel.cs and services (GameDetectionService.cs, ProcessMonitorService.cs, ProcessService.cs) to align with the refactor and improve robustness (timeouts, fallbacks, logging and error handling). Reason: reduce MainWindow.xaml.cs size, improve separation of concerns and maintainability, and introduce more resilient startup/shutdown and tray/update logic.
1 parent c9253f8 commit 594a3c4

8 files changed

Lines changed: 3546 additions & 3483 deletions

MainWindow.Behaviors.partial.cs

Lines changed: 1771 additions & 0 deletions
Large diffs are not rendered by default.

MainWindow.xaml.cs

Lines changed: 1 addition & 1730 deletions
Large diffs are not rendered by default.

Models/ProcessModel.cs

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -22,36 +22,6 @@ namespace ThreadPilot.Models
2222

2323
public partial class ProcessModel : ObservableObject
2424
{
25-
private Process? process;
26-
27-
public Process? Process
28-
{
29-
get => this.process;
30-
set
31-
{
32-
this.process = value;
33-
if (value != null)
34-
{
35-
this.ProcessId = value.Id;
36-
this.Name = value.ProcessName;
37-
try
38-
{
39-
this.ProcessorAffinity = (long)value.ProcessorAffinity;
40-
this.Priority = value.PriorityClass;
41-
this.MemoryUsage = value.PrivateMemorySize64;
42-
this.ExecutablePath = value.MainModule?.FileName ?? string.Empty;
43-
this.MainWindowHandle = value.MainWindowHandle;
44-
this.MainWindowTitle = value.MainWindowTitle ?? string.Empty;
45-
this.HasVisibleWindow = this.MainWindowHandle != IntPtr.Zero && !string.IsNullOrWhiteSpace(this.MainWindowTitle);
46-
}
47-
catch (Exception)
48-
{
49-
// Process may have terminated or access denied
50-
}
51-
}
52-
}
53-
}
54-
5525
[ObservableProperty]
5626
private int processId;
5727

Services/GameDetectionService.cs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,15 +72,17 @@ public GameDetectionService(
7272

7373
public async Task<ProcessFeatures> ExtractProcessFeaturesAsync(ProcessModel process)
7474
{
75+
var (threadCount, handleCount) = GetRuntimeProcessMetrics(process.ProcessId);
76+
7577
var features = new ProcessFeatures
7678
{
7779
ProcessName = process.Name,
7880
ExecutablePath = process.ExecutablePath ?? string.Empty,
7981
HasVisibleWindow = process.HasVisibleWindow,
8082
CpuUsage = process.CpuUsage,
8183
MemoryUsage = process.MemoryUsage,
82-
ThreadCount = process.Process?.Threads.Count ?? 0,
83-
HandleCount = process.Process?.HandleCount ?? 0
84+
ThreadCount = threadCount,
85+
HandleCount = handleCount
8486
};
8587

8688
try
@@ -122,6 +124,19 @@ public async Task<ProcessFeatures> ExtractProcessFeaturesAsync(ProcessModel proc
122124
return features;
123125
}
124126

127+
private static (int ThreadCount, int HandleCount) GetRuntimeProcessMetrics(int processId)
128+
{
129+
try
130+
{
131+
using var liveProcess = Process.GetProcessById(processId);
132+
return (liveProcess.Threads.Count, liveProcess.HandleCount);
133+
}
134+
catch
135+
{
136+
return (0, 0);
137+
}
138+
}
139+
125140
public async Task<GamePerformanceMetrics> GetGamePerformanceAsync(ProcessModel process)
126141
{
127142
var metrics = new GamePerformanceMetrics

Services/ProcessMonitorService.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -403,7 +403,7 @@ private async Task HandleProcessStartedAsync(EventArrivedEventArgs e)
403403
var processName = e.NewEvent["ProcessName"]?.ToString() ?? string.Empty;
404404

405405
// Get detailed process information
406-
var process = await this.CreateProcessModelFromId(processId, processName)
406+
var process = await this.CreateProcessModelFromId(processId, processName).ConfigureAwait(false)
407407
?? (!string.IsNullOrWhiteSpace(processName)
408408
? new ProcessModel { ProcessId = processId, Name = NormalizeProcessName(processName) }
409409
: null);

Services/ProcessService.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -523,7 +523,7 @@ public async Task<IEnumerable<ProcessModel>> GetProcessesByNameAsync(string exec
523523
{
524524
return Enumerable.Empty<ProcessModel>();
525525
}
526-
});
526+
}).ConfigureAwait(false);
527527
}
528528

529529
public async Task<bool> IsProcessRunningAsync(string executableName)

0 commit comments

Comments
 (0)