views:

1251

answers:

4

Tools : SilverLight 2, C# 3.5, VS2008 and WCF Async Programming

In my SL2 application, I am making 3 async wcf calls as follows -

void HomeScreen()
{

//Async WCF Calls
DataService.GetPersonInfo(sUser);
DataService.GetSalaryInfo(sUser);
DataService.GetDepartmentInfo(sUser);

//Where to put this code?
//Page.Redirect("MainScreen");

}

After all 3 async calls has been completed i need to move user to a MainScreen. How do i know that all 3 async calls has been completed ?

(without using loop to check global variable for async method status)

Does SL2 has any inbuilt feature like Jquery to know all async call has been completed for ex -

$().ajaxStop($.unblockUI);

Any Thoughts ?

A: 

Assuming that theses calls can't be made again while you're waiting, you could just add a Completed handler for each of the services. In each handler, you could set a flag for that call and them check to see if all three flags have been set. If so, then go to the main screen.

Jacob Adams
+1  A: 

Similiar to Jacob, abstract this away into a separate class. This will at least simplify your calling class and remove the complexity of joining the calls.

In your class, in the simplest terms simply on each completed event from the async call, check how many events have completed, if it matches the total, fire a completed event of your own. Somethign like this:

public class DataProvider()
{
  private int callCount = 0;

  public event EventHandler Completed;
  public void Go()
  { 
    callCount = 0;
    //Async WCF Calls
    DataService.GetPersonInfo(sUser);
    DataService.GetSalaryInfo(sUser);
    DataService.GetDepartmentInfo(sUser);
  }

  public void GetSalaryInfoCompleted(object sender, SomeArgs e)
  {
    //Do something with the results here
     CheckIfCompleted();
  }
  public void GetDepartmentInfoCompleted(object sender, SomeArgs e)
  {
    //Do something with the results here
     CheckIfCompleted();
  }

  public void GetPersonInfoCompleted(object sender, SomeArgs e)
  {
     //Do something with the results here
     CheckIfCompleted();
  }

  private void CheckIfCompleted()
  {
     callCount++;
     if ( callCount == 3 )
     {
       Completed(this, EventArgs.Empty);
     }

  }

}
Ray Booysen
+3  A: 

DataService.GetPersonInfo(sUser, (result) => { DataService.GetSalaryInfo(sUser, (result) => { DataService.GetDepartmentInfo(sUser, (result) => { Page.Redirect("MainScreen"); } ); } ); } );

But your GetPersonInfo method will have to change it like

GetPersonInfo(user, Action complete){ //And call complete.Invoke(true); or false based on completing of the processs. }

Hope it helps.

Michael Sync
oh. Rich Text Editor here remove \n :(
Michael Sync
A: 

Setting flags certainly is a useful solution, but this thread shows some more refinded approches to the problem of "synchronizing" asynchronous calls.

Britney Spears