Tuesday, May 30, 2017

Freezable Objects in WPF


Freezable objects are special type of object which can be use to increase performance.
Freezable objects have two state unfrozen and frozen. In unfrozen state object is work like normal object but in frozen state object can be modified.

Freezable classes are drive from Freezable
as you can see in image SolidColorBrush class is drive from Freezable class.



In WPF mostly graphics sub-system are related to Freezable, and can help to increase performance, most of these class generate low level object to render their graphics stuffs such as background color for buttons and when their is any change in these object (e.g. Brush) these low level object again regenerated, even if there is no actual change even wpf graphics system spend some resources to monitor if there is any change.

So if object is in frozen state, WPF graphics system need not to monitor the modification andas a result it increase performance.

Let understand this with a simple example :
we have a button and we assign a SolidColorBrush object to the button background, the information what we provide to SolidColorBrush to show the background, not generate any color, for this a low level object is generate for the button and the brush and that object appear on the screen , and WPF graphic system spend resource to monitor the change , if there  is any change it again re-generate the object. so when we make the objecr Unfrozen this monitor got ignored.

example of Freezable object
Bursh, Pen, Geometry, Transform, AnimationTimeline

How to Freeze the Freezable object

Use Freeze() method to make a object Frozen

            SolidColorBrush redbrush = new SolidColorBrush(Colors.Red);
            redbrush.Freeze();

Check for Freezable object can Freeze

It is very easy to do this use CanFreeze() method to check object can freeze or not.
            if (redbrush.CanFreeze)
            {
                redbrush.Freeze();
            }

How to check object is Freeze or not
            
           if (redbrush.IsFrozen)
           {
              ...
           }

Below is complete example

            Button btnFreezable = new Button();
            SolidColorBrush redbrush = new SolidColorBrush(Colors.Red);
            btnFreezable.Foreground = redbrush;
            if (redbrush.CanFreeze && redbrush.IsFrozen)
            {
                redbrush.Freeze();
            }

            try
            {
                redbrush.Color = Colors.Yellow;
            }
            catch (InvalidOperationException ex)
            {
                MessageBox.Show("Invalid operation: " + ex.ToString());

            }

Freezable Object has changed event which fire where there is any modification in Object or Object it Contains.
need example

Feature of Freezable object
  • It provide change event to notify when modification in object.
  • It can improve performance if make it UnFrozen
  • Only a Frozen Freezable object can share across the thread


This Post will continue for further topic

1) How to use Freezable from xaml
2) CloneCore
3) CloneCurrentValueCore
4) GetAsFrozenCore
5) GetCurrentValueAsFrozenCore
6) FreezeCore

No comments:

Post a Comment