I have a Windows Form application that implements both BackgroundWorker and standard Threading.
System.ComponentModel.BackgroundWorker
System.Threading.Thread
The BackgroundWorker objects are used to update the UI. The classes I made that run the long processing are threaded and have events that are raised as updates are made and when the threads complete. The BackgroundWorker sits and waits for these raised events, then updates the UI so the user can see the progress. All pretty straight forward stuff.
I want to take this to the next level and implement processor throttling. Some of these actions can take many minutes, while potentially others could take hours. I want the machine to continue to be usable by other processes. The application actually runs on a server and runs in two different modes; application mode and windows service mode. In both situations, I want to throttle the use of the processors. The server could have 1 or more processors running. My application is multi-threaded, so it could potentially use all the processors on the machine and max them all out.
I reviewed few articles that said to use System.Threading.Thread.Sleep(xx) to limit processor usage. I think it is a bad design, and I also think it would be crazy and very time-consuming to build that line into all of my loops and processing routines. I also read some reviews that said to use System.Diagnostics.Process to get the current process and set the priority to low, then sleep, then set it to boost priority when you run it. Again, I would have to make a LOT of entries throughout my processing routines to add that intelligence. There just has to be a better way.
Isn't there a way to System.Diagnostics.Process or another namespace to throttle how much my program is using at any time Ideally, I would like to look at how much the processor(s) are being used and determine if I want to increase my processing during periods of inactivity. Example; the server is processing 100's of users during the day and requires 50% of the processor so my program uses say 15% of the available power. Then at night when there aren't any users, it notices the system idle process and boosts its usage to maybe 80% or so.
Can anyone offer and good suggestions What am I missing Is it not possible to tell your program to use 30% of processor availability, then scale it back or up if necessary Thanks in advance for your assistance.

Throttling a Windows Form Application
Cryo75
Curtis Gray
Thank you for your post. Yes, you are correct. Any other thread with a higher priority would execute over my threads, but it doesn't solve the issue that I want my thread to always be able to use about 10% of the processor all the time. In your scenario, if the server is being used harder during the day, there is the possiblity that my threads will rarely execute, if even at all. I want to dedicate 10% of the processor during the day so it has at least that much to use, then allow it to use as much as it wants at night when the processor isn't being used as much. Any thoughts on this
As a side bar; is it possible to determine how much the machine's processors are being used at any given moment
Steve Dunn
Ianmac
Great, I think the information in that link will allow me to create my own Processor watching code and bump the priority of my threads as necessary.
Is there also a way to throttle the memory usage of my threads
Poma
Richard Purchas
Check this thread for code to use the PerformanceCounter class.