views:

101

answers:

3

I'm trying to execute a long-running piece of code in a "background" thread and by "background" I mean low-priority thread not .NET background term. I created a thread, set its priority to Lowest and still 100% CPU is used if no other threads are running. The situation improves when I manually call Thread.Sleep(1) but I don't want to change my code and controlling CPU utilization via Thread.Sleep is highly unaccurate.

I want some way of executing a piece of code which will be throttled to ensure CPU usage at 5%-10%. Also I don't want to change the code too much (it is quite complex).

Is it possible?

+1  A: 

It sounds a bit like you need to solve the wrong issue - instead of trying to throttle the thread to a set CPU usage (which I'm not sure you can do), is there something wrong with what the thread is doing/how it's doing it? i.e. if it uses 100% CPU, is there nothing you could do to that logic to make it less CPU intensive in the first place?

You may find you can rework/optimise it to prevent it being this intensive in the first place, hence solving the problem at the root.

UPDATE: periodically read the external sources using a timer. A good reference is here. You will solve the root cause of the high CPU usage by not continually checking those sources, but by doing it periodically (e.g. even if you do it every 15 seconds say, that will make a vast difference)

AdaTheDev
I can add some Thread.Sleep but this is kind of messy. The process itself reads data from external sources and repopulate memory cache. I do not know when the data has been changed until I read it, so the "background" process is constantly enumerating/reading all data sources and refreshed memory cache. Data does not have to be up-to-date (I mean a few minutes delay is acceptable) but it needs to be highly accessible (hence data are always served from the memory cache).
Karol Kolenda
This is the real problem - the constant reading of the data sources. The best solution is to only do it periodically, you can use a timer to do this. I'll update my answer to include a reference.
AdaTheDev
Thank you AdaTheDev - I'll read the referenced article and try to chop the code into more manageable chunks.
Karol Kolenda
A: 

Setting the thread priority is not an absolute thing. It's relative to other threads in the program.

Setting the thread priority to Normal will mean that it gets an equal "share" of the processor time (with other Normal threads).

Setting the priority to Low (or Lowest) will mean that it gets less of the processor time compared to Normal threads in the same program, and setting it to High (or Highest) will mean that it gets more.

If there is only one thread then it will get as much CPU as it needs regardless of the priority you set.

As AdaTheDev says, if it's using 100% CPU then the issue is with your algorithm not how Windows is allocating processor time to the thread.

ChrisF
+1  A: 

Perhaps you should have your "background" thread check a shared boolean that is set when the high-priority thread is working and wait until it's unset.

ebpower