1

I have a Silverlight class library that references a WCF service, I have a method called StoreNodes() that call the WCF service. like this:

public void StoreNodes()
{    
    DataServiceClient client = new DataServiceClient();
    client.GetNodesForCoreCompleted += client_GetNodesForCoreCompleted;
    client.GetNodesForCoreAsync();
}

and another method called BuildAll() like this:

public void BuildAll()
{
    StoreNodes();
    Method2();
}

My problem is method2() is not a WCF service, and the both StoreNodes and Method2 have some variables in common, these variables get their values in StoreNodes and the second method do some operation on them, anyway, the problem is the methode2 is executed before the first method finishes, so I got the null reference error. How can I make sure that the second method is executed after the service calling is finished?? I hope I made my question clear.

Delimitry
  • 2,987
  • 4
  • 30
  • 39
AboKevo
  • 115
  • 2
  • 2
  • 12
  • 3
    What's stopping you from calling Method2 in client_GetNodesForCoreCompleted? – villecoder Apr 25 '12 at 19:58
  • @villecoder, you should post your comment as the answer :) – Shiraz Bhaiji Apr 25 '12 at 20:12
  • @villecoder : i thought about that , but its not the point, thanks for your time :) – AboKevo Apr 25 '12 at 20:26
  • 1
    @AboKevo I'm not sure what you're asking, then. You're trying to call Method2 after the WCF method completes, right? Why not call in your method that's been designated to execute after the WCF call completes? – villecoder Apr 25 '12 at 20:42
  • @villecoder : i know ur absolutely right , and your way is 100% working , but for some reason i don't want to do that, if nothing else worked i'll do as u said , and change my design for the class library :D – AboKevo Apr 25 '12 at 20:50

2 Answers2

2

Something along these lines should work:

public void BuildAll()
{
    StoreNodes(() => { Method2(); });
}

public void StoreNodes(Action getNodesCompleteAction)
{    
    DataServiceClient client = new DataServiceClient();

    client.GetNodesForCoreCompleted += (sender, e) => {
      // your handler code

      // call Method2() Action wrapper
      getNodesCompleteAction();
    }

    client.GetNodesForCoreAsync();
}
Dmitry Samuylov
  • 1,554
  • 2
  • 14
  • 37
1

There are two ways that you could solve this problem.

First, you can set up a type of async method chaining. After each method completes, it calls the next one:

StoreNodes();
|
V
 client_GetNodesForCoreCompleted
  |->Method2();

And, you could continue this...

   |
   V
   client_Method2Completed
    |->Method3();
    ....

The other way is that you could put some sort of wait inside of Method2 that will wait until the appropriate variables are set. However, this is against the general programming paradigm of Silverlight, so I would not suggest it realistically. It would lock your UI while you simply wait for a resource. IF, this is what you want, then here is a SO answer on how you could wait for the values

Community
  • 1
  • 1
Justin Pihony
  • 66,056
  • 18
  • 147
  • 180