Refactor process tracking to handle null slots and compact array
This commit is contained in:
@@ -120,14 +120,32 @@ internal static class Program {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private static void Track(Process process) {
|
private static void Track(Process process) {
|
||||||
|
// First find the first null slot
|
||||||
|
int firstNullIndex = -1;
|
||||||
for (int i = 0; i < NumProc; i++) {
|
for (int i = 0; i < NumProc; i++) {
|
||||||
if (windows[i] == null) {
|
if (windows[i] == null) {
|
||||||
windows[i] = process;
|
firstNullIndex = i;
|
||||||
Console.WriteLine(
|
break;
|
||||||
$"Added {process.ProcessName} to tracked windows at index {i}");
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (firstNullIndex == -1) {
|
||||||
|
Console.WriteLine("No slots available for tracking");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Compact the array by shifting non-null elements to the left
|
||||||
|
for (int i = firstNullIndex + 1; i < NumProc; i++) {
|
||||||
|
if (windows[i] != null) {
|
||||||
|
windows[firstNullIndex] = windows[i];
|
||||||
|
windows[i] = null;
|
||||||
|
firstNullIndex++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Add the new process at the first null slot
|
||||||
|
windows[firstNullIndex] = process;
|
||||||
|
Console.WriteLine($"Added {process.ProcessName} to tracked windows at index {firstNullIndex}");
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void TrackWows() {
|
private static void TrackWows() {
|
||||||
|
Reference in New Issue
Block a user