Debugging/profiling/optimizing a WPF application

Mots clés Technorati : WPF,Debugging,Profiling,Optimizing

In this post I will describe and redirect you to good articles or good tools for.

I will talk about four main points:

  • Debugging bindings;
  • Checking your visual tree;
  • Profiling your WPF application;
  • Optimizing your WPF application;

Data bindings

Here are two very important posts that explicit different solutions for debugging bindings:

- This post from Bea Stollnitz;

- This post from Karl Shifflet;

After reading those post, in WPF projects I use all those techniques in this order:

  1. I read the Visual Studio Output Window to show. In most cases, the message is enough explicit.
  2. Usage of the DebugConverter: here is the implementation of the DebugConverter I use in projects.

Code Snippet

  1. public class DebuggingConverter : IValueConverter
  2.   {
  3.       public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
  4.       {
  5.           System.Diagnostics.Debugger.Break();
  6.           return value;
  7.       }
  8.  
  9.  
  10.       public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
  11.       {
  12.           System.Diagnostics.Debugger.Break();
  13.           return value;
  14.       }
  15.   }

I define it in the resources of the application, and it’s done. I just have to call this converter on the binding where I encounter an issue. Automatically the debugger breaks in my converter. After solving the binding issue, I suppress the converter and that’s all.

I don’t really use the trace sources because most of times, I already found the issue before using it Sourire

Visual Tree debugging

In a previous post, I explained you my favorite tools, the two I use quickly to debug the visual tree of my application are Mole and Snoop.

Mole is useful when you set a breakpoint in Visual Studio and you want to debug the visual/logical tree of your application. It’s integrated into Visual Studio, when in your code you set a breakpoint, you can easily open Mole to understand what is happening in the visual or in the logical tree. I use Mole in Visual Studio, when debugging. I’m able to quickly find what’s happening –> incorrect owner, style not applied correctly, …

Snoop is different tool, also displays the visual tree of a WPF application, it is not working within Visual Studio. It takes the applications at a specific moment. It also displays in a tree control the visual tree to easily navigate into the elements but also allows to split the application in a 3D representation of all visual elements to better understand the graphical composition of the application. This allows to to better understand why part of your application is not docked correctly or stretched correctly.

Profiling tools

Performance Profiling Tools for Windows Presentation Foundation (also know as WPFPerf) is available in the Microsoft Windows SDK for Windows 7 and the .NET Framework 4.

WPFPerf contains 2 tools :

  • Perforator : analyze rendering behavior, this is used to look for rendering issues;
  • Visual Profiler : analyze the use of WPF services to determine which visual element if causing a performance bottleneck.

I really use them when I really want to understand what is happening on a UI point of view, how the visual tree is build, how all the events/animations are

If you need more documentation on those tools, I redirect you to  WPF Performance Suite on MSDN .

Optimization reference

Here are my two main resources for optimization

Do not forget that WPF is also a part of .NET, so also use the Improving .NET Application Performance and Scalability form Pattern & Practice.

Conclusion

In this post I summarized some actions to do during a development life cycle on a WPF project.

And of course, you can follow a TechDays 2011 Session, from one of my colleague Luc Vo Van on WPF/Silverlight performance tips & tricks.