I have a function which will load children of a Node. Internally it calls a WCF async service to load. The signature is as follows.
public void AddChildElements(Node parentElement,
Action<IEnumerable<Node>> callback)
{
}
This can be used like
_nodeBuilder.AddChildElements(parentElement, (elements) =>
{
//do something with elements
});
Now I want to write a function to expand the hierarchy based on some condition. So I write a extension function like this
public static T LoadHierarchyUntilItemFound<T>(
this IEnumerable<T> sequence, Func<T, List<T>> loadaction,
Func<T, string> searchstring)
{
//...
}
The loadaction parameter expects the loading function for the node.The usage is as follows
Elements.LoadHierarchyUntilItemFound(LoadChildren,
"root.UK.England.London");
The problem is how will I write a loading function?
private List<Node> LoadChildren(Node parent)
{
// _nodeBuilder.AddChildElements(parent, here it expects a callback
//which gives the result, how to use this?);
}
In short, the trouble is how can I use a callback function to return the result for a wrapper function?
If you really want it is possible to just make a function that calls the async method and blocks until it returns:
However, in many contexts it would be preferable to do this entire thing asynchronously, rather than synchronously.
Given a function like that you can call it using: