How to limit CPU resources for a desktop application

vituxa used Ask the Experts™
Hello Experts,

I have built a Windows desktop application that reads through a bunch of files and folders and analyzes them, and creates image previews while it loops through them.

Problem is that sometimes, when the file/folder structure is too large, it is taking nearly 100% of the system processor and RAM. I was able to successfully limit the RAM to take maximum 20% of the system's total, but I cannot manage to limit the CPU allowed resources.

Can you please share a piece of code  with me that will do the trick?
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Senior Software Developer
In Windows you can't "partition" the CPU usage so it maxes out at a certain value or percentage. That's solely the job of the operating system. You can, however, reduce the priority of your process so that other tasks that are running get preferred treatment. Just put this code in your program somewhere that it will run when the program first starts.
            System.Diagnostics.Process thisProc = System.Diagnostics.Process.GetCurrentProcess();
            thisProc.PriorityClass = System.Diagnostics.ProcessPriorityClass.BelowNormal;

Open in new window

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial