I have the following snippet of code (as an example) that looks up a contact:
public string Search()
{
string address = "";
ContactManager manager = new ContactManager();
// LookupComplete is just a plain event
manager.LookupComplete += delegate
{
address = manager.Address;
};
manager.SearchFor("bob");
return address; // Address always appears to be populated
}
Update:
Here's the ContactManager:
public class ContactManager
{
public string Address {get;set;}
public event LookupComplete;
public void SearchFor(string query)
{
SomeParser parser = new Parser();
parser.TokenParsed += new EventHandler<TokenParseEventArgs>(tokenParsed);
parser.Parse(query);
}
private void tokenParsed(object sender,TokenParseEventArgs e)
{
if (e.Message == "EOF")
{
Address = e.Message.Address;
if (LookupComplete != null)
LookupComplete(this,EventArgs.Empty);
}
}
}
The Search
method doesn't return until that event is fired (a behaviour I didn't realise was standard for anonymous methods/delegates).
I'm confused however how the code generated for the anonymous delegate signals the Search
method when it's complete.
I've tried putting a Sleep(5000) in the ContactManager.Address property as I thought it may have been from the ContactManager simply returning very quickly, but that makes no difference.
Can anyone shed any light?
This has nothing to do with whether the delegate was created with an anonymous method. It's just that
manager.SearchFor
is apparently a synchronous method, which doesn't return until the lookup has finished. That makes it slightly strange for it to have an event for "lookup complete", admittedly... it looks like you could simplify your method to simply:Now it's possible that there's also an asynchronous option available on
ContactManager
- it's hard to say without knowing more about the class. If there is an asynchronous option, you should consider what behaviour you actually want... are you happy withSearch
being synchronous, or do you actually want it to complete immediately and take some action when the search has finished?