Solved

Running a background task with max CPU.

Posted on 2014-03-03
10
608 Views
Last Modified: 2014-03-05
Hi

I have a VB.NET application that starts a background task defined as a  System.Threading.Thread. The background task is only analysing data that is already in memory, but it may take hours to finish. When I look at the CPU utilisation it never seams to get more that 50% CPU (only one thread) even if the rest is free. On PC's with a 4 threaded CPU it is often as low as 25%.

Is it possible to adjust this so that the background task gets all (or at least a lot more than 50%) of the CPU and run it as a low priority task?

I have looked around and there seams to be possible to control this from both inside the application and directly within Windows, but what is the best way to control this?

Does anyone know how to configure this?
0
Comment
Question by:daghoff
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
10 Comments
 
LVL 34

Expert Comment

by:Michael-Best
ID: 39901114
0
 
LVL 13

Expert Comment

by:Naman Goel
ID: 39901180
A single-threaded process with any priority can consume 100% of CPU time on one core.

When you have two processes, one with higher priority than the other, that would both consume 100% CPU time on their own, executing simultaneously, the one with higher priority will get all of the CPU time (assuming a single core) and the one with lower priority will not progress at all.

The priority of a process is simply used to determine the order in which the scheduler will "hand out" CPU time slices to processes.

The other thing you can do is by increasing the priority of Process

 System.Diagnostics.Process.GetCurrentProcess().PriorityClass = System.Diagnostics.ProcessPriorityClass.RealTime

Open in new window

0
 

Author Comment

by:daghoff
ID: 39901185
Thanks, but it's not that easy.

I have set the priority to high, but the CPU allocated is still only 25%. The PC I am now testing on has a 4 threaded CPU. I belive there is a setting somewhere restricting a background tread to only have the same CPU % as one thread can deliver.

Any ideas?
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:daghoff
ID: 39901220
Thanks Naman Goel for the second reply, but what is the difference compared to what Michael-Best sayes?

Using System.Diagnostics.Process.GetCurrentProcess().PriorityClass = System.Diagnostics.ProcessPriorityClass.RealTime is something you specify inside the VB.NET code I asume? It will stille be the same as setting it in the Task Manager, or is it not?

Anyway I was not alowed to set the priority to Real-Time in the Task manager, it defaulted to High.

Are you absolutly certain it can be solved by just increasing the priority only?
0
 
LVL 13

Expert Comment

by:Naman Goel
ID: 39901228
Background threads do not have lower priority. The difference between foreground and background threads is that the CLR will shutdown the process once no more foreground threads are running. Thread pool threads are background threads.

 thread.Priority = ThreadPriority.AboveNormal
0
 
LVL 13

Expert Comment

by:Naman Goel
ID: 39901245
That should be because it will increase the priority of your process above other process. But still it all depends on your logic that is running on Thread, if you are using sleep and all then anyways it will not consume much CPU.

A logic like simple while loop will consume maximum CPU Time as it doesn't have to wait for IP or other task.
0
 

Author Comment

by:daghoff
ID: 39901277
Please find my background tread enclosed.

It does not wait for anything. It is only comparing values as fast as it can that are already in memmory. It does write some staus info on the screen controled by the forground task, but that is only ocasionally. And keep in mind the CPU is alwayes 25% and that is all the PC does. There is a restriction enforeced somewhere.

I will test out setting the priority in the code as suggested and rerun a recompiled version.
DClass.txt
0
 

Author Comment

by:daghoff
ID: 39901362
I have tried both System.Diagnostics.Process.GetCurrentProcess().PriorityClass = System.Diagnostics.ProcessPriorityClass.RealTime & thread.Priority = ThreadPriority.AboveNormal, but it has no effect.

Am I doing something wrong here?
Where and how should these parameters be set?
0
 
LVL 55

Assisted Solution

by:McKnife
McKnife earned 250 total points
ID: 39902590
It has nothing to do with priorities. Your script does only use one core. If you could change it to use multiple cores, of course the total percentage would go higher accordingly. But don't ask me how to code that.
0
 
LVL 83

Accepted Solution

by:
CodeCruiser earned 250 total points
ID: 39902900
You have some contradiction requirements. You want this thread to be background and low priority but still use maximum CPU when the purpose to mark a thread low priority is to ensure that it does not hog the CPU.

Further to what McKnife alluded to above, you need to use Parallels library(requires .NET 4 and above) to enable your thread to use all cores on the processor.

http://msdn.microsoft.com/en-us/library/dd460720(v=vs.100).aspx
0

Featured Post

Raise the IQ of Your IT Alerts

From IT major incidents to manufacturing line slowdowns, every business process generates insights that need to reach the people required to take action. You need a platform that integrates with your business tools to create fully enabled DevOps toolchains.

You need xMatters.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Today, still in the boom of Apple, PC's and products, nearly 50% of the computer users use Windows as graphical operating systems. If you are among those users who love windows, but are grappling to keep the system's hard drive optimized, then you s…
When you try to extract and to view the contents of a Microsoft Update Standalone Package (MSU) for Windows Vista, you cannot extract the files from the MSU. Here we are going to explain how to extract those hotfix details without using any third pa…
The viewer will learn how to successfully download and install the SARDU utility on Windows 7, without downloading adware.
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

691 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question