How to reduce CPU usage of a WPF application?

My WPF application used high CPU usage after about 30 minutes, after which I break the application to find out which code spent a lot of CPU, but I didnโ€™t get anything.

Visual Studio 2008 cannot display the current executable code, but I found this in the Call Stack panel:

  [In a sleep, wait, or join] 
 mscorlib.dll! System.Threading.WaitHandle.WaitAny (System.Threading.WaitHandle [] waitHandles, int millisecondsTimeout, bool exitContext) + 0x8f bytes 
 System.dll! System.Net.TimerThread.ThreadProc () + 0x2f9 bytes    
 mscorlib.dll! System.Threading.ThreadHelper.ThreadStart_Context (object state) + 0x66 bytes   
 mscorlib.dll! System.Threading.ExecutionContext.Run (System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state) + 0x6f bytes    
 mscorlib.dll! System.Threading.ThreadHelper.ThreadStart () + 0x44 bytes   

what is it? What is important when using a processor? and how to reduce CPU usage?

+4
source share
4 answers

We used the Performance Profiling Tool for WPF / Visual Profile to find out which events require the most CPU usage. Tick โ€‹โ€‹(TimeManager.Tick ()) occupied about 40% of the CPU usage by the application. then we removed all the animated controls one by one; finally, we found that the storyboard increased CPU usage in about 30 minutes.

then we changed the form:

calendarStoryboard.Begin(txtMessage, HandoffBehavior.Compose, true); 

to

 calendarStoryboard.Begin(txtMessage, HandoffBehavior.SnapshotAndReplace, true); 

This problem has been fixed. the more information about HandoffBehavior, refer to msdn:

http://msdn.microsoft.com/en-us/library/system.windows.media.animation.handoffbehavior.aspx

+4
source

You should take a look at other threads, I think that the switch for displaying streams is in the debug menu of the visual studio. โ€œ[Standby, Standby, or Connectionโ€ means that the thread cannot do anything because it is waiting for another thread to complete it.

It can be stuck in an infinite loop somewhere, intentionally or not (intentionally, for example, some UI things are constantly redrawn, for example, animation or something like that). This is not in the current thread shown on your stack.

+1
source

You have several options for tracking the problem. I would start with the performance wizard in Visual Studio 2008. You will find it in the Analysis menu.

+1
source

I'm not a WPF expert, but the call stack that you show here is probably not your problem. This thread expects some other synchronization object and does not do any work. The reason VS cannot display the current code is because it expects in its own code (after calling WaitAny (), I believe that you are calling your own OS construct that does the actual wait).

Are there any other threads in your WPF process that can use CPU time?

0
source

All Articles