Solved

Running a background task with max CPU.

Posted on 2014-03-03
10
604 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
Industry Leaders: 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 54

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

If you get continual lockouts after changing your Active Directory password, there are several possible reasons.  Two of the most common are using other devices to access your email and stored passwords in the credential manager of windows.
When you try to share a printer , you may receive one of the following error messages. Error message when you use the Add Printer Wizard to share a printer: Windows could not share your printer. Operation could not be completed (Error 0x000006…
In this video, we discuss why the need for additional vertical screen space has become more important in recent years, namely, due to the transition in the marketplace of 4x3 computer screens to 16x9 and 16x10 screens (so-called widescreen format). …
In this Micro Tutorial viewers will learn how to use Boot Corrector from Paragon Rescue Kit Free to identify and fix the boot problems of Windows 7/8/2012R2 etc. As an example is used Windows 2012R2 which lost its active partition flag (often happen…

726 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