ScottG
ScottG

Reputation: 11111

Calling a method and waiting for a return value

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.

Upvotes: 4

Views: 7411

Answers (5)

John Saunders
John Saunders

Reputation: 161831

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.

Upvotes: 2

Michael Meadows
Michael Meadows

Reputation: 28426

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.

Upvotes: 0

Sören Kuklau
Sören Kuklau

Reputation: 19970

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.

Upvotes: 4

Reed Copsey
Reed Copsey

Reputation: 564901

Most asyncronous methods return IAsyncResult.

If yours does, you can use the IAsyncResult.AsyncWaitHandle to block (IAsyncResult.AsyncWaitHandle.WaitOne) to block until the operation completes.

ie:

bool aBool;

IAsyncResult res = client.CallAnAsyncMethod(id); res.AsyncWaitHandle.WaitOne(); // Do something here that computes a valid value for aBool! return aBool;

Upvotes: 8

Bob King
Bob King

Reputation: 25866

Can you simply call the web method synchronously?

Upvotes: 3

Related Questions