How to limit CPU utilization in .NET app?

G

Guest

I have a Windows service that's built with .NET. The process is meant to run
in the background and not suck up too much CPU time. I set the thread
priority for the various threads in the service to Below Normal, but I'm
still getting reports that the service is making a noticable impact on
overall machine performance.

I stuck some Thread.Sleep calls in a various places in the code, but that
doesn't help much, since the service makes single API calls that are fairly
processor intesive (such as computing the checksum on a file) and can't be
broken down any further.

Is there some API (.NET or otherwise) or something that will allow me to to
say "Don't ever use more than 10% of the user's CPU time"?

Thanks
 
K

Kevin Yu [MSFT]

Hi,

First of all, I would like to confirm my understanding of your issue. From
your description, I understand that you need to limit the CPU utilization
in your windows service. If there is any misunderstanding, please feel free
to let me know.

As far as I know, there isn't such method to achieve this in .NET framework
or through Win32 API. Since CPU utilization is a caculated number by the
operating system, we can't simply tell the app that "Don't ever use more
than 10% of the user's CPU time", because the application doesn't know
anything about the utilization.

Setting the thread priority to a lower level might help. But it only makes
the thread sleep when there are other high priority threads available. If
no other threads, this thread also takes high CPU usage, if needed.

I think the only thing we can do is to make some Thread.Sleep call with
long sleep time. However, if there is a single CPU consuming API call in
the app, I don't think we can do much to avoid this.

HTH.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."
 
K

Kevin Yu [MSFT]

You're welcome. Thanks for sharing your experience with all the people
here. If you have any questions, please feel free to post them in the
community.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top