I have a webservice method getContactsAsync. I’ve understood that when it’s completed it’s supposed to raise an event. How can I (still asynchronously) find out when the method is done and use the result?
public void GetContacts()
{
webService.getContactsAsync()
}
public void GetContactsCompleted(object sender, getContactsAsyncCompletedEventArgs e)
{
contacts = e.Result;
}
I don’t get how I’m supposed to use this. The way I can think of is to call GetContacts and then with a while-loop check if contacts != null to know when it has completed. But this will lock up the thread.
Is there some kind of best-practice typical for this situation?
Thanks for reading!
If I’m completely out of it feel free to explain how it actually works 🙂
Each
DoSomethingAsyncmethod has a correspondingDoSomethingCompletedevent that is raised when the method finishes. (See Event-based Asynchronous Pattern Overview for more information.) You need to attach your event handler method to thewebService.GetContactsCompletedevent in order for it to be executed:(Note that you will need to ensure that the event handler is only attached once, or it will start being invoked multiple times when the method finishes.)