Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Running a background task with max CPU.

Posted on 2014-03-03
10
Medium Priority
?
623 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
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

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 57

Assisted Solution

by:McKnife
McKnife earned 1000 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 1000 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

Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

Question has a verified solution.

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

Simulator games are perfect for generating sample realistic data streams, especially for learning data analysis. It is even useful for demoing offerings such as Azure stream analytics, PowerBI etc.
Hello there! As a developer I have modified and refactored the unit tests which was written by fellow developers in the past. On the course, I have gone through various misconceptions and technical challenges when it comes to implementation. I would…
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…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
Suggested Courses

971 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