-1

I'm building a small program that checks if the resource monitor is open and if so, closes it.

bool a = true;
while (a == true)
{
    foreach (var p in Process.GetProcessesByName("perfmon"))
    {
        Process.Start("taskkill", "/F /IM perfmon.exe");

        // "perfmon" "Taskmgr"
        foreach (var t in Process.GetProcessesByName("taskkill"))
        { 
             ... 
        }
    }
}

The problem is that it takes almost 30% of my CPU performance, so how can I reduce the CPU usage?

Also I tried to add other programs like tasklist and task manager but it didn't work, do you have an idea?

bool a = true;
while (a == true)
{
    System.Threading.Thread.Sleep(1000);
    foreach (var p in Process.GetProcessesByName("Taskmgr"))
    {
        Process.Start("taskkill", "/F /IM Taskmgr.exe");

        // "perfmon" "Taskmgr"
        foreach (var t in Process.GetProcessesByName("taskkill"))
        {
            System.Threading.Thread.Sleep(60000);

            Process b = Process.Start("");
        }
    }

    foreach (var p1 in Process.GetProcessesByName("perfmon"))
    {
        foreach (var t1 in Process.GetProcessesByName("taskkill"))
        {
            System.Threading.Thread.Sleep(60000);

            Process b1 = Process.Start("");
        }
    }

    foreach (var p2 in Process.GetProcessesByName("tasklist"))
    {
        foreach (var t2 in Process.GetProcessesByName("taskkill"))
        {
            System.Threading.Thread.Sleep(60000);

            Process b2 = Process.Start("");
         }
    }
}

Jeanot Zubler
  • 979
  • 2
  • 18
torlab
  • 1
  • 1
  • 4
    Does this answer your question? [CPU friendly infinite loop](https://stackoverflow.com/questions/7402146/cpu-friendly-infinite-loop) – devsmn Dec 21 '21 at 13:56
  • The question title and body do not match. You should probably update the title make clear that you are asking about CPU usage and not memory. – JonasH Dec 21 '21 at 14:40
  • Have you considered using machine policies in winows itself to disable opening of task manager instead of writing a program to close it when it sees it. https://social.technet.microsoft.com/Forums/ie/en-US/35dd3cd6-cc67-476f-82e2-058293e6f657/how-do-i-disable-task-manager-for-users-only?forum=winserverTS – Scott Chamberlain Dec 21 '21 at 16:18
  • I dont want to to disable task manger, Process.Start("taskkill", "/F /IM perfmon.exe"); can be replace by Process.Start("taskkill", "/F /IM chrome.exe"); or whatever – torlab Dec 21 '21 at 17:11

1 Answers1

0

Put a Sleep() in your while-loop:

System.Threading.Thread.Sleep(1000);

Your program is constantly checking all processes on your PC. If it does so, only once every second, that's more than enough.

Dominique
  • 16,450
  • 15
  • 56
  • 112
  • I tried before and it didn't work but now yes, so thanks. But when I check I have enought time to see my other program (which get closed) and because it takes more than 0 percent, my program is on the top in the task manager . I checked that link https://stackoverflow.com/questions/7402146/cpu-friendly-infinite-loop Do you think I could introduce it inside my program ? (I know it's a bit of assistantship, sorry). – torlab Dec 21 '21 at 14:38