I use CompositionTarget.Rendering a lot in the bag-o-tricks. (Last count: AnimatingTilePanel, Graph, ZapDecorator, and FlipTile3D.)

Rob posted to an internal discussion about how to use (and how not to use) the event. (I saw a link on WiredPrairie as well.)

Specifically, it's important to remember this is a timer that ticks all of the time--every frame--potentially 60 times a second or more.

Since it's a static event, it will keep firing even if control you use it in isn't on the screen any more.

Hence, it's critically important that you manage the use of this event appropriately.

  • Un-register the event when your control is unloaded. I actually add a handler for the Unloaded event in my controls to do this. If you don't do this, events will still be fired to your delegate, even if the control is removed from the visual tree! This is not only a perf hit, but could introduce crazy behavior, crashes, data loss, mass hysteria, etc.
  • Potentially un-register the event when the UI isn't visible. Like when the window is minimized. Be careful, though. Finding window state from a control could be ugly. You also may want to let your animation run its course even if it's not visible, which leads me to the final bullet...
  • Un-register when your animation "settles". Since I used the Rendering event for my physics models, I do this when I reach equilibrium. Once all of the forces have balanced out and stuff stops moving, I un-register the event (and make sure to re-register once something changes that will start things moving again).

Users don't like their CPU pegged (unless something really cool is happening). A user with a dead laptop battery is not a happy user.

Happy hacking!