How to deal with CPU usage in WPF application ?

By | March 16, 2011

If you are building a WPF application with lots of animation inside it, may be some of which runs forever, you must have been a problem of eating up your entire CPU or a mammoth portion of CPU while the program is running.

The problem is because by default, the framerate for WPF application is set to 60 per second. Thus for every second if your application has slight change, WPF environment draws frames for 60 times and eventually take up lot of CPU in doing so.

To deal with such scenario, you should always set TimeLine.DesiredFrameRate = 20 or anything that suits you.

<EventTrigger RoutedEvent="Mouse.MouseEnter">
<BeginStoryboard >
<Storyboard Timeline.DesiredFrameRate="20">
<DoubleAnimation Duration="0:0:2"
Storyboard.TargetProperty="RenderTransform.ScaleX" To="3"
<DoubleAnimation Duration="0:0:2"
Storyboard.TargetProperty="RenderTransform.ScaleY" To="3"

In this Trigger, you can see the StoryBoard.DesiredFrameRate is set to 20. You can set its value between 1 to 99, but anything below 10 will give performance hiccups for your application.

If you have already built your application and want to change the default behavior of TimeLine.DesiredFramerate, you can use :


new FrameworkPropertyMetadata { DefaultValue = 10 }

Set this on Application_Startup in App.xaml and you will get the performance bonus immediately for your application.

One thought on “How to deal with CPU usage in WPF application ?

  1. Pingback: Dew Drop – March 18, 2011 | Alvin Ashcraft's Morning Dew

Comments are closed.