A few weeks ago, a client asked me to come in to have a look at their Silverlight-application, since they were facing some problems with the asynchronous calls to services. In particular: how do you go about blocking your UI when such a call is being made? Mind you, I don’t mean the type of “blocking” that hangs the UI, I mean the type that blocks the user from continuing or entering invalid info 🙂
As I wrote a while ago: when you face problems like this, DO NOT try to attempt to make the service call behave as if it were synchronous. Why? Well, in short, it never rrrreeallly works, and it kind of defeats the purpose of using async calls to avoid an application that feels like it’s hanging 🙂
So, what can you do? What if you have, for example, a button on your application which should not execute it’s handler unless a few conditions are true, and what if one of those conditions is validated through a service? Say you have a textbox, and depending on the value inputted in that textbox, the button should be clickable or not. But: it takes some time to return from the async call checking the value in the textbox, so a fast user just might be able to click your button, even though the value in the textbox validates to false, meaning the button shouldn’t be clickable. In a non-async world, you could simply put your validation rules behind the button click, but in the async SL-world, this isn’t an option anymore – if one of the validation rules requires a service call, you’re stuck.
Don’t fear though 🙂 A few really simple rules apply to make sure your async calls won’t pose problems when concerning user input, validation, etc. This post will outline (some of) ’em, and provide you with a downloadable example project at the end of this post.
A small example project using a few of these techniques can be found here. Hope this helps some of you out! 🙂