1

I am working on WPF/C# desktop application.

I have 2 methods (lets call Tasks). Each task takes 10% of the CPU and whole application takes 10% as well. So,

  1. When I run both tasks in one instance, it divides the CPU utilization (5% for each, so obviously processes get slow)

  2. When I run 2 instances of my application, and run one task on each application. Its all fine. Total utilization is 20% as should be for 2 tasks.

Now the question is: How can I use 20% of CPU utilization in one instance of my application?

I can not add code, because its very complex to make a MCVE of my application.

Complete Scenario:

Its a face recognition application. 1st camera with 30 fps utilizes 10% CPU. When 2nd camera starts, both fps gets down to 15 fps and the total application utilization is still 10% (it doesn't increase but slows down the tasks, as it did with fps).

When I run 2 instances of the application, each with 1 camera. FPS is 30 on both instances and CPU utilization is 20% in total. That's the whole scenario !

Community
  • 1
  • 1
Shaharyar
  • 12,254
  • 4
  • 46
  • 66

1 Answers1

2

First of all, the CPU usage will depend on what you are doing in the methods that are running asynchronously. If there are operations that are awaiting for external resources to come (like if you have threads that are fetching data from the internet), or if there are many I/O operations, then the CPU usage might not be high because the thread will mostly wait for data to come instead of doing calculations. You can expect a high CPU usage on a thread when you do massive calculation operations. You could possibly change the code that is running asynchronously to increase the CPU usage - this is a very general remark, but no other can be given as we don't see your code.

Secondly - if you are using Threads directly in your code, then there is a Priority property on the Thread class, you can try to increase it - but you have no guarantee that the CPU usage will grow. Also setting a high priority for a thread in order to increase the CPU usage you need to remember that you are taking a risk that other applications running on the same machine might be "starved" (i.e. their performance will be decreased because of your code).

msporek
  • 1,187
  • 8
  • 21
  • I am not using any async process which can be slow, I've explained a bit about the scenario. I hope now it'll be easier to understand the problem. – Shaharyar Dec 20 '14 at 21:34
  • Well, that's a different thing now after your edit than it was. But we don't know how you deal with the data from the cameras in your application. You said about two methods, how do you know that each of them takes 10% of the CPU? That sounds like they are working paralelly. How do you call the two methods? Without a piece code I cannot give any more advice. – msporek Dec 20 '14 at 21:38
  • I wish I could create minimal working example. I mentioned these are two `tasks` i am dealing with the camera. Actually utilization doesn't matter. But whatever I explained in the question is my analysis, I've checked it all on almost 4 to 5 different configuration systems (utilization % changes on every system but it keeps same for both processes) and fps gets down to 15 on every system. – Shaharyar Dec 20 '14 at 21:46
  • I can't help you really as you're not providing the basic info to make it possible to help you. You say "tasks", if you're using the System.Threading.Tasks.Task and async, then I don't know if there is a way to increase a CPU usage with that. With Thread class you do as I have written in my answer. If you're using a different programmatic solution for dealing with the cameras data - I have no idea. – msporek Dec 20 '14 at 21:49
  • I am using `Parallel.Invoke` to call a method which initiates the camera and starts grabbing frames from it. – Shaharyar Dec 20 '14 at 21:52
  • There is no way you can force it to consume more CPU. Try the solutions using Thread class and a high priority, a lot of examples are available online. – msporek Dec 20 '14 at 21:54
  • Can you refer any example? – Shaharyar Dec 20 '14 at 21:57
  • Actually I am very beginner at concurrent programming. and I read in an article that tasks are easy to handle as compare to threads. As you know the scenario now, so what would you suggest me to use for it? – Shaharyar Dec 20 '14 at 21:57
  • Obviously I did before coming here. After an hour of searching I was still no where. – Shaharyar Dec 20 '14 at 21:59
  • I said you should use Thread class, http://msdn.microsoft.com/en-us/library/aa645740%28v=vs.71%29.aspx, http://msdn.microsoft.com/en-us/library/7a2f3ay4(v=vs.90).aspx. When you do it with Thread class, make it a background thread (IsBackground property set to true). – msporek Dec 20 '14 at 22:00