views:

13

answers:

1

I have a Silverlight 4 application using RIA services. One of the RIA service methods does something similar to the following:

List<Foo> fooList = this.GetListOfFoo();
AnotherService aService = new AnotherService();
foreach (Foo foo in fooList)
{
   aService.SomeMethodCompleted += this.methodCompleted;
   aService.SomeMethodAsync(foo);
}

return fooList;

SomeMethodAsync then makes changes to foo, which is a reference type.

this.methodCompleted is called for each Foo, but the updates to the Foo instances are not seen by the Silverlight client. I'm guessing nothing automatically waits for all of the async calls to finish before the RIA service returns to the Silverlight client.

My question is: can I coordinate the completion of the async calls before returning to the Silverlight client? Or am I limited to making only synchronous calls in a RIA service?

A: 

The answer is: do it the way you would for any WCF or web service method. That is, an array of WaitHandles that each call to methodCompleted sets, and WaitAll just before returning. Or a CountDownEvent if you're making >64 async calls, like I might need to.

And don't wire up the Completed event handler INSIDE the foreach. Otherwise you'll call your Completed handler (fooList.Count)^2 times, which messes with your CountDownEvent :)

James McLachlan

related questions