Asynchronous Lambda Method calls

You won’t get any benefit using this code, but it makes for more manageable source. So from Pete Browns blog:

All network calls in Silverlight are asynchronous. The proliferation of event handling functions this can cause just makes for really cluttered code. Using lambda expressions seems to make my code a bit cleaner. (You can also use regular anonymous delegates, but the lambdas involve less typing)

public void LoadPolicyDetail()
{
IvcDataServiceClient client = new IvcDataServiceClient();

client.GetPolicyDetailCompleted += (s, e) =>
{
if (e.Result != null)
{
_policyDetail = e.Result;

if (PolicyDetailLoaded != null)
PolicyDetailLoaded(this, new EventArgs());
}
else
{
if (PolicyDetailLoadError != null)
PolicyDetailLoadError(this, new EventArgs());
}
};

client.GetPolicyDetailAsync();
}

The event handler is right in-line with the loading function. The event args remain strongly typed, so you have access to everything you normally would with an additional function.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

A WordPress.com Website.

Up ↑

%d bloggers like this: