0

So, my brother was playing Fortnite and it was lagging quite a bit. So I offered to make an application that will limit the CPU usage of other apps, but I actually am having trouble with getting the limit to go on the other application.

Here's the code I've tried:

public void ThrottledLoop(Action action, int cpuPercentageLimit)
        {
            Stopwatch stopwatch = new Stopwatch();

            while (true)
            {
                stopwatch.Reset();
                stopwatch.Start();

                long actionStart = stopwatch.ElapsedTicks;
                action.Invoke();
                long actionEnd = stopwatch.ElapsedTicks;
                long actionDuration = actionEnd - actionStart;

                long relativeWaitTime = (int)(
                    (1 / (double)cpuPercentageLimit) * actionDuration);

                Thread.Sleep((int)((relativeWaitTime / (double)Stopwatch.Frequency) * 1000));
            }
        }

Please help, if there is any other information you need just let me know. Thanks

  • That's something the operating system would have to do, so your application would have to make a request into that. – UnholySheep Dec 06 '21 at 23:30
  • FYI: A much simpler solution is to just set the game's process priority to a higher value, instead of trying to set everything else lower – UnholySheep Dec 06 '21 at 23:32
  • Task manager -> end Task :) or https://windowsreport.com/task-manager-set-priority/ – TheGeneral Dec 06 '21 at 23:38
  • oh i can do that? @UnholySheep – TheComputerWizard Dec 06 '21 at 23:41
  • but what if i have unsaved stuff open? @TheGeneral – TheComputerWizard Dec 06 '21 at 23:41
  • Or, thinking outside the square, just get a pc with more cores https://memegenerator.net/img/instances/71853357.jpg – TheGeneral Dec 06 '21 at 23:43
  • You can also specify what **processors** (_"affinity"_) processes are allowed to run on. This offers more fine grain control than setting process priority. e.g. limit them to say the _last 4 cores_ on a system with 16 logical cores but allowing Fortnite to use whatever it wants. Be aware, some apps might not take too kindly to it. For an example take a look at the Windows _Task Manager_ **Details** tab. https://stackoverflow.com/questions/2510593/how-can-i-set-processor-affinity-to-a-thread-or-a-task-in-net. –  Dec 06 '21 at 23:43
  • wait but what if i wanted to turn that handful of apps to 1 or 2 cores ONLY when the game is open? – TheComputerWizard Dec 06 '21 at 23:48
  • @TheComputerWizard _"wait but what if i wanted to turn that handful of apps to 1 or 2 cores ONLY when the game is open?"_ I thought you wanted me to post it as an answer. Please don't go changing the essence of the question after an answer has been posted –  Dec 07 '21 at 10:47
  • okay sorry... im just really bad at making questions (i only do it when nothing else is helpful) – TheComputerWizard Dec 22 '21 at 05:45

1 Answers1

0

Summary from comments above.


You can specify what processors ("affinity") processes are allowed to run on. This offers more fine grain control than setting process priority.

e.g. limit certain processes to say the last 4 cores on a system with 16 logical cores but allowing Fortnite to use whatever it wants. Be aware, some apps might not take too kindly to it.

Anti-virus programs sometimes play nice by keeping themselves at the end of the list of cores in a system.

For an example take a look at the Windows Task Manager Details tab.

See also