Solved

Running a background task with max CPU.

Posted on 2014-03-03
10
582 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
 

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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
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 53

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Windows 8 comes with a dramatically different user interface known as Metro. Notably missing from the new interface is a Start button and Start Menu. Many users do not like it, much preferring the interface of earlier versions — Windows 7, Windows X…
This Micro Tutorial will teach you how to the overview of Microsoft Security Essentials. This is a free anti-virus software that guards your PC against viruses, spyware, worms, and other malicious software. This will be demonstrated using Windows…

706 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now