Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9255919
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T11:49:07+00:00 2026-06-18T11:49:07+00:00

I have a function which will load children of a Node. Internally it calls

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T11:49:08+00:00Added an answer on June 18, 2026 at 11:49 am

    If you really want it is possible to just make a function that calls the async method and blocks until it returns:

    private List<Node> LoadChildren(Node parent)
    {
        var tcs = new TaskCompletionSource<IEnumerable<Node>>();
    
        AddChildElements(parent, nodes => tcs.SetResult(nodes));
    
        return tcs.Task.Result.ToList();
    }
    

    However, in many contexts it would be preferable to do this entire thing asynchronously, rather than synchronously.

    public static async Task<T> LoadHierarchyUntilItemFoundAsync<T>(
        this  IEnumerable<T> sequence, Func<T, Task<IEnumerable<T>>> loadaction,
        Func<T, string> selector, string searchstring)
    {
        var stack = new Stack<T>(sequence);
        while (stack.Any())
        {
            var item = stack.Pop();
    
            if (selector(item) == searchstring)
                return item;
    
            foreach (var child in await loadaction(item))
            {
                stack.Push(child);
            }
        }
    
        return default(T);
    }
    

    Given a function like that you can call it using:

    Elements.LoadHierarchyUntilItemFound(AddChildElements,                               
                                   "root.UK.England.London"); 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have function some_func_1 which will create an object of type some_type and will
I have a Function in CFC file, which will be called from .cfm file
All, I have the following array and function, which will create a multidimensional array
I have a jquery function loading a php file which will output some data
i have a button1_click() function which runs on page load,,now i want to call
I am working on asynchronous calls with $.getScript which will load the scripts I
The wierdest thing. I have a function which calls: var clicked_el = event.target.id; This
I have a Webview in which I first load an URL, which will automatically
I have a function which will resize the image background when the page is
hi i have function which is called by tinker listbox so i cannot return

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.