CPU Usage Becomes Very High During Execution
I recently built a service uses a single thread to process information periodically. The service was built using Microsoft Visual Studio 2010 Express. I noticed the CPU usage took off to the moon (CPU usage at 100%).
Later, out of curiosity, I built a very small program, in which all it does is start a small thread, or timer, then runs some incredibly long loop. I also switched from building a console application to a Windows Form application. I was able to reduce my trouble by switching the type of program to build.
I've tried lowering the priority of the thread:
System.Threading.Thread.CurrentThread.Priority = ThreadPriority.Lowest;
I've also tried to set the priority class
Process p = Process.GetCurrentProcess();
p.Prio开发者_开发技巧rityClass = ProcessPriorityClass.BelowNormal;
I also thought that the CPU usage could've been high due to running on a virtual machine. But that theory didn't take me anywhere. It's pretty ridiculous. I don't know where else to turn. I'm not sure why starting a timer or a thread would send my CPU usage through the ceiling the way it does in the .NET framework. I never experienced this when building COM objects. Does anyone have any bright ideas as to where I can turn?
Thanks for your time in advance.
Sounds like your code is looping infinitely and unexpectedly.
Start it in the Visual Studio debugger and when you see the high CPU, hit 'Break' to pause execution. The callstacks and other program state you see when you do this repeatedly should give you an idea of what is wrong with your logic.
It's possible that simply stepping through the code in the debugger would be quicker. It depends on how complex the code is.
If you're looping repeatedly in your thread it could definitely cause high CPU usage. Have you tried adding a pause to your thread execution between loops?
Thread.Sleep(1000);
精彩评论