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.
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;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With