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 how I discovered the problem, but I'll tell you what the problem was, and how we solved it.

The application

In our application we have dynamic content that is requested asynchronously to a web service. While the request is made, we show a small spinning donut. When the request is done, we remove the spinning donut and replace it with the content we want to show. The spinning donut is a tiny xaml with an animation that loops forever.

The problem

When the donut 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 spinning donut, we stop the animation. That solved the problem.

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.