How to deal with CPU usage in WPF application ?

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.

Abhishek Sur

Abhishek Sur is a Microsoft MVP since year 2011. He is an architect in the .NET platform. He has profound theoretical insight and years of hands on experience in different .NET products and languages. He leads the Microsoft User Group in Kolkata named KolkataGeeks, and regularly organizes events and seminars in various places for spreading .NET awareness. He is associated with the Microsoft Insider list on WPF and C#, and is in constant touch with product group teams. He blogs at His Book : Visual Studio 2012 and .NET 4.5 Expert Development Cookbook. Follow Abhishek at Twitter : @abhi2434

One Comment to “How to deal with CPU usage in WPF application ?”

Comments are closed.