I have a program that runs on a Terminal server. The server has roughly 40 thin clients.
The program's core function locks the desktop icons in a specified layout, and if they are moved, they will "snap" back into place.
The issue is within the infinite loop that constantly moves the icons back in place. I currently have the loop end each cycle with a
Thread.Sleep(100);
When I change the sleep to
Thread.Sleep(10000);
Then the CPU usage dramatically decreases. (the contents within the loop eats the resources)
What I need is to optimize this loop so that when the loop runs, it uses less CPU resources.
Please let me know if you need any other information.
Edit: The script is deployed as a .exe that runs on user login. The user is blocked from ending the service.
Here is the loop:
while (true)
{
try
{
// Refresh desktop handle in case explorer instance changed.
DesktopHandle = FindWindow("Progman", null);
if (DesktopHandle == IntPtr.Zero)
{
continue;
}
DesktopHandle = FindWindowEx(DesktopHandle, IntPtr.Zero, "SHELLDLL_DefView", null);
if (DesktopHandle == IntPtr.Zero)
{
continue;
}
DesktopHandle = FindWindowEx(DesktopHandle, IntPtr.Zero, "SysListView32", null);
if (DesktopHandle == IntPtr.Zero)
{
continue;
}
// Get the collection of list items in the SysListView32 control woooo scaryyyyy.
AutomationElement sysListView = AutomationElement.FromHandle(DesktopHandle);
AutomationElementCollection listItems = sysListView.FindAll(
TreeScope.Children, new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.ListItem));
// Clear the Ico2Idx dictionary and rebuild it to reflect the current state
Ico2Idx.Clear();
for (int i = 0; i < listItems.Count; i++)
{
Ico2Idx.Add(listItems[i].Current.Name, i);
}
foreach (string Icon in Icons)
{
// Check if the icon exists on the desktop before trying to move it.
if (Ico2Idx.ContainsKey(Icon))
{
// Move icon by name
if (ListView_SetItemPosition(DesktopHandle, Ico2Idx[Icon], Ico2Coord[Icon].X, Ico2Coord[Icon].Y) != null)
{
// Console.WriteLine(String.Format("Icon \"{0}\" should be set now!", Icon));
}
// *May* need to slow here, test and see.
}
}
Thread.Sleep(10000);
}
catch (Exception ex)
{
// Log the exception and continue with the loop.
Console.WriteLine($"An error occurred: {ex.Message}");
}
}