Hi,

I have created a Form C# application that has alot of code inside. Runs on QuadCore processor.

The problem consumes about 2-5% CPU in general when looking in TaskManager.

Problem:
At some point the application consumes about 60-70% and stucks there forever. So I am very sure that
somewhere it gets stuck in an infinite loop.

Question:
It is almost impossible to know where in all this code, this is happening. I do run the application in debug mode
when developing.

How is it possible to detect where in an application CPU is very consuming like in this scenario, to find the
piece of code that has the problem?

Run in debug mode and monitor CPU usage. When it jumps up, use the debug menu to 'break' the code and see where it is.

Okay, I am not sure how to do that. In the debug menu, then only break option I can see is "Toggle Breakpoint" but that just highlight a row in code?

Also, as this application is highly multithreaded, there are a lot of processes going on at the same time.
So I wonder how "break" in code can be "intelligent" to actually find the code that consumes like in this case probably 90-95% CPU of the whole application?

Debug menu should look like this while running, you'll want the 5th option. And VS handles multi-threading (which might be your problem, people don't understand threading very well and often misuse it).

My debug menu doesn´t look like that exactly. The 5:th options in your menu is "Stop Debugging", did you meen "Break All" ?

I run Microsoft Visual C# 2010 Express

My menu look like this:

Yes, I mean "Break All", not sure what was going on in my head then :)

Hmm, not sure VS 2010 Express has the threaded debugger. You might want to include some logging in your code and see what comes up often.

I don't know, but there should be profilers like valgrind or Quantify (IBM) for .NET applications. Have you tried any of those?

Yes I have some logging in the code but but it is quite difficult to know where to put it because it could be 100:s of places.

A Profiler sounds like a good idéa. I have never used one before but I have downloaded this one:
http://www.yourkit.com

It can profile the CPU usage and show hotspots. I will give it a try and hope the application reproduce the problem. Stack trace/threads are possible to see on high CPU usage.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.