C# Disciples

my life in Avalon ….

WPF: Reducing CPU consumption for animations

Lately I’ve been working on a project at work and the application was performing pretty well. The CPU Usage was below 2% and I was quite happy with it. Then I decided to add a simple animation that continues executing forever… the results 15% CPU consumption … I was amazed at how much CPU was being wasted. Yet I am a stubborn guy and if I want an animation to execute forever, I will do it no matter what….

I started researching and I found out that WPF animations by default try to serve a 60 frames per second… Which is quite a lot!!! After having a chat with my friend ( and role model ) DR.WPF, it was all clear. (Thanks Dr for all the help you give me !!!)

So in order to reduce the frame rate per second (thus reducing CPU usage), all you need to do is to override the property meta data of the Dependency property Timeline.DesiredFrameRateProperty…. So basically all you need is 1 line of code (I usually put this code on startup of the application)

   1: Timeline.DesiredFrameRateProperty.OverrideMetadata(
   2:             typeof(Timeline),
   3:             new FrameworkPropertyMetadata { DefaultValue = 20 }
   4:             );

Yep, that’s it… Amazing but true…. I managed to make an application that was consuming 15% CPU to consume 2% CPU….

With regards to what frame rate should you use, I would leave that to you…. This depends on how smooth you want your animations to perform… Yet for me 20 is working quite fine…

Hope this helps

kick it on DotNetKicks.com

About these ads

June 13, 2008 - Posted by | WPF

16 Comments »

  1. Great work!

    Comment by Josh Smith | June 13, 2008 | Reply

  2. coolio

    Comment by sacha | June 14, 2008 | Reply

  3. [...] tipp innen [...]

    Pingback by Túl sok CPU-t eszik a WPF animáció? - VBandi | June 14, 2008 | Reply

  4. Thanks, some of my projects can really use this one to.

    Comment by Bragi | June 15, 2008 | Reply

  5. Hi Marlon,

    Great article, well done!!!

    Rudi Grobler

    Comment by Rudi Grobler | June 17, 2008 | Reply

  6. [...] Marlon Grech posted an easy way to reduce CPU consumption for WPF animations. [...]

    Pingback by Visual Studio Links #40 : Visual Studio Hacks | June 22, 2008 | Reply

  7. Marlon,

    Very nice!

    Cheers,

    Karl

    Comment by Karl Shifflett | June 23, 2008 | Reply

  8. Hi!
    I have been prove it and it works perfectly!!

    It was great… pass from 20% cpu usage to 10%…5%…

    Comment by wilmerbz | September 20, 2008 | Reply

  9. thanks!!!

    Comment by wilmerbz | September 20, 2008 | Reply

  10. [...] kom jeg over et innlegg i en blogg som beskriver hvordan redusere FPS, som igjen fører til mindre CPU-forbruk. Settes FPS for lavt, [...]

    Pingback by Overganger på bilder i WPF bruker mye CPU - Ole A. E. | January 11, 2009 | Reply

  11. thanks! that helped a lot, specially when testing the app in a virtual machine.

    Comment by isel | January 28, 2010 | Reply

  12. Hi,

    I have a window without borders and transparencykey is true due to which the window gets its elliptical shape. I have a simple animation where I’m changing the opacity of an image and the repeatbehavior is forever. The CPU consumption is above 80% even after implementing your solution. Is there another way of bringing the CPU usage down?

    Thanks in advance!!

    Comment by Anupam Sharma | February 4, 2010 | Reply

  13. HI , This is very clear and elaboratice article on FPS. I appreciate your step to share and guide others.

    Zahid

    Comment by Zahid | July 17, 2010 | Reply

  14. 谢谢,太棒了
    Thx, Good job!

    Comment by Nick Chen | February 7, 2011 | Reply

  15. Saved my day.

    Comment by vhanded | February 21, 2011 | Reply

  16. where am I to put this code? I tried putting at the start of Main windows definition I had compile errors. I tried at the beginning of the rotation method (before all the rotations started) and I got an exception. What should I do???

    Comment by macrian | November 17, 2012 | Reply


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 845 other followers

%d bloggers like this: