I'm trying to write a method that grabs a contact, and converts it into a data object and returns it. I understand that since the contact search is asynchronous, the calling method needs to be also; however, I want to return an object as part of the parent method that's calling the search and I'm not quite sure what the best approach is.
The pseudocode I've got at the moment is:
public Person GetRandomPerson(string s)
{
Person myPerson = new Person();
Person contacts = new Contacts();
contacts.SearchCompleted += new EventHandler<ContactsSearchEventArgs>(contacts_SearchCompleted);
contacts.SearchAsync(String.Empty, FilterKind.None, "All Contacts");
return Person; //I'm not sure how this will ever work...
}
void contacts_SearchCompleted(object sender, ContactsSearchEventArgs e)
{
//convert a Contact object to Person object here
}
I've done a bit of reading around the place (like this SO question) but I'm not quite sure how a nested asynchronous returning call would look like, or how I'd pass the result from the event based asynchronous contact search back onto the parent calling method - How would I achieve something to this effect?