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
Solved

Running a background task with max CPU.

Posted on 2014-03-03
10
600 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
Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

 

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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Add a Condition in ASP Repeater 6 33
VB.net Open video relating to control 2 19
Cannot install image with GHOST 4 24
Amazon S3 Images with .Net 3 10
The recent Microsoft changes on update philosophy for Windows pre-10 and their impact on existing WSUS implementations.
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
This Micro Tutorial will teach you the basics of configuring your computer to improve its speed. It will also teach you how to disable programs that are running in the background simultaneously. This will be demonstrated using Windows 7 operating…
This Micro Tutorial will give you a introduction in two parts how to utilize Windows Live Movie Maker to its maximum editing capability. This will be demonstrated using Windows Live Movie Maker on Windows 7 operating system.

792 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