Performance issues in Silverlight 2 Animations
Performance issues in Silverlight 2 Animations
I had to fix a problem in a Silverlight application we are building. The problem was that it’s CPU usage was constant, between %20 and %40, and never stopped. I won’t go into the details of on how much time it took me to understand the reason of the problem, but I’ll tell you what the problem was, and how we solved it.
Scenario :
A dynamic content application that requests asynchronously a web service. While the request is made, the application shows a small spinning animation. When the request is done, the application removes the spinning animation and replaces it with the content. The spinning animation is composed of a tiny XAML and loops forever.
The problem:
When the animation is removed from the page, the animation keeps looping until the garbage collector reclaims it, and that might take a long time. So the CPU usage was wasted due to that invisible animation which was no longer used.
The solution:
Before replacing the animation with the content, I had to stop the animation. That solved the problem.
The Why:
Unfortunately, Silverlight controls doesn’t implement IDisposable, so you can’t just stop the animation in a Dispose method. You have to do this manually.
I hope Silverlight makes each DependencyObject or UIElement implement IDisposable in the near future.