views:

697

answers:

5

How do I call a method that returns a bool, but inside that method in order to determine the value of the bool, it calls a web service asyncronously?

bool myBool = GetABoolean(5);    

public bool GetABoolean(int id)
{

  bool aBool;

  client.CallAnAsyncMethod(id);  // value is returned in a completed event handler.  Need to somehow get that value into aBool.

  return aBool;  // this needs to NOT execute until aBool has a value

}

So what I need is for the GetABoolean method to wait until CallAnAsyncMethod has completed and returned a value before returning the bool back to the calling method.

I'm not sure how to do this.

+3  A: 

Can you simply call the web method synchronously?

Bob King
+7  A: 
Reed Copsey
+3  A: 

Either call the method synchronously, or add an CallAnAsyncMethodCompleted event handler for it, and use the e.Result object to work with the return value.

Sören Kuklau
+2  A: 

This is at least the third time in recent days I've seen this sort of question, so I'll ask some questions (the answer to the OP is obvious).

Why are you considering calling the web service asynchronously? Performance? Not blocking a worker thread?

Have you seen an example that leads you to use an async web service call? If so, could you post the URL of the example?

Thanks for your time. Your answers to these questions will help me answer others.


I added a "silverlight" tag to this question, and I suggest you do the same yourself in the future, for Silverlight questions. That creates the context that all the "blocking" calls must be asynchronous.

Now, a SilverLight expert should answer, but I think you're going about this the wrong way. You're losing the asynchronous nature of the call. I think that your GetABoolean method should be asynchronous as well, and should not return until it has the answer.

John Saunders
I'm trying to port this WPF code over to Silverlight and need the calls to be async calls
Scott
Thanks for the input. I have lots of code like this where I have a method that returns a value. Inside that method it makes a service call. Previously that call was sync. I need to now make it async...but I need it to sit there in that method until the service call returns.
Scott
Again, "no, you don't". Make the calling method async, and you don't have to sit in it.
John Saunders
A: 

If there's more code between the call to CallAnAsyncMethod and the return, then there's potential value in doing it asynchronously (although it's likely an unnecessary preoptimization), otherwise synchronize your code.

If you don't have any control over an architecture that's forcing you to do this code asynchronously, you'll have to monitor a variable and loop to wait for completion.

bool myBool;
bool retrievingMyBool;
RetrieveABoolean(5);    

public bool RetrieveABoolean(int id)
{
    client.CallAnAsyncMethod(id);  // value is returned in a completed event handler.  Need to somehow get that value into aBool.
    retrievingMyBool = true;
    while (retrievingMyBool)
    {
        Thread.Sleep(100);
    }
}

private void completedEventHandler([[parameters go here]])
{
    // code to handle parameters
    myBool = // whatever
    retrievingMyBool = false    
}

This is a horrible solution, and will cause you gigantic headaches in the future, especially if you ever need the code to be thread safe, but as a hack, it could work.

Michael Meadows