This is a mirror of official site: http://jasper-net.blogspot.com/

Deal with Performance in WPF applications

| Friday, April 8, 2011
Hi friends,

WPF is one of the major changes to the desktop applications in recent times. Most of us is using it in your day to day life programming. Some use for normal desktop or windows based applications while others write programs that run in browsers as Sandboxed application. The major investments of Microsoft on making silverlight to work out of browser enhanced the usage of WPF in a larger extent. But as for any other application, performance is the major issue for your application. Its not how well you structured your application, or how loose couple your UI with other layers, it is often a requirement for any software on how it acts in stressed situations.

One of such performance hits that might appear for your WPF application is over utilization of CPU. In this post, I will cover some of the general performance hiccups with WPF which you might consider unnecessary for your application or may be you want just to identify them for your application. Lets put few of the performance improvement tips for you in this post : 

Try not to be Smarter

WPF architecture is well written to handle performance of your application in smart way. It draws only the portion of the screen which is visible through the window. Hence if you have code which hides an element in WPF window, the Frames which WPF rendering engine draws will not take up those elements.

Hence it is unnecessary to Remove a child element from a Container (even with having a strong reference of the control for future) to improve the application performance, rather it is ok if you just hide the control visually.For instance, Let us suppose you have a TextBox on the screen, now for certain situation if you want to hide the TextBox from the screen, it will not be an issue to hide the Textbox rather than actually removing the TextBox from the Visual Tree.

Basically WPF runs with two threads (not only) which you should know. 

1. UI Thread  : Sometimes called as Dispatcher Thread is actually a Thread that runs all the managed code within it. This thread creates each element of WPF and every control has Thread affinity for the thread. Every control in WPF inheriting from DispatcherObject does holds the reference of this Thread in the form of DispatcherThread property. Hence if you want to run any Non-UI thread to invoke a statement, like if any of your non-ui thread needs to update any WPF element, you should use code like this :

this.Dispatcher.BeginInvoke((Action)delegate()
{
    //Write your Code here
}, DispatcherPriority.ApplicationIdle);

Read more: DOT NET TRICKS

Posted via email from Jasper-net

0 comments: