Tuesday, December 6, 2011

Taming Silverlight Async Calls

Users of Silverlight often fall foul of the Asynchronous calling pattern which is enforced by the framework. Among the pitfalls are:
  • Callback methods being called multiple times instead of once
  • Attempts to use information before it has been retrieved
  • Inability to link a specific call to a specific callback
A number of articles have been written on CodeProject and elsewhere about this issue. Indeed, there are even forthcoming changes to the language to make it easier to handle some common cases. However, all of the approaches I've seen to date either require you to redesign your approach, or enforce a 'pseudo synchronous' approach in which the benefits of having multiple async calls in flight simultaneously are lost.

The AsyncCalls library addresses these problems and more. It offers
  • An easy to read way of writing simple 'pseudo sync' method calls
  • The ability to have several calls to the same method in progress at the same time, even llowing a different callback method to be run for each call, and guaranteeing that the user code will run exactly once for each async invocation
  • The ability to configure multiple calls to multiple async methods in such a way that as many of them as possible can be in progress at the same time, while guaranteeing that all needed results are available

And the whole library is only a page long - smaller than the demo Silverlight app included in the download!