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

  • Home
  • SEARCH
  • 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 4103476
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T20:57:48+00:00 2026-05-20T20:57:48+00:00

Suppose I have the following class: Public class FooBar { List<Items> _items = new

  • 0

Suppose I have the following class:

Public class FooBar
{

   List<Items> _items = new List<Items>();

   public List<Items> FetchItems(int parentItemId)
   {

     FetchSingleItem(int itemId);

     return _items
   }

   private void FetchSingleItem(int itemId)
   {

   Uri url = new Uri(String.Format("http://SomeURL/{0}.xml", itemId);
   HttpWebRequest webRequest = (HttpWebRequest)HttpWebRequest.Create(url);

   webRequest.BeginGetResponse(ReceiveResponseCallback, webRequest);

   }

   void ReceiveResponseCallback(IAsyncResult result)
   {

     // End the call and extract the XML from the response and add item to list

     _items.Add(itemFromXMLResponse);

     // If this item is linked to another item then fetch that item 


     if (anotherItemIdExists == true)
     {
        FetchSingleItem(anotherItemId);
     }


   }

}

There could be any number of linked items that I will only know about at runtime.

What I want to do is make the initial call to FetchSingleItem and then wait until all calls have completed then return List<Items> to the calling code.

Could someone point me in the right direction? I more than happy to refactor the whole thing if need be (which I suspect will be the case!)

  • 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-05-20T20:57:49+00:00Added an answer on May 20, 2026 at 8:57 pm

    Getting the hang of asynchronous coding is not easy especially when there is some sequential dependency between one operation and the next. This is the exact sort of problem that I wrote the AsyncOperationService to handle, its a cunningly short bit of code.

    First a little light reading for you: Simple Asynchronous Operation Runner – Part 2. By all means read part 1 but its a bit heavier than I had intended. All you really need is the AsyncOperationService code from it.

    Now in your case you would convert your fetch code to something like the following.

     private IEnumerable<AsyncOperation> FetchItems(int startId)
     {
         XDocument itemDoc = null;
         int currentId = startId;
    
         while (currentID != 0)
         {
            yield return DownloadString(new Uri(String.Format("http://SomeURL/{0}.xml", currentId), UriKind.Absolute),
                 itemXml => itemDoc = XDocument.Parse(itemXml) );
    
            // Do stuff with itemDoc like creating your item and placing it in the list.
    
            // Assign the next linked ID to currentId or if no other items assign 0
    
         }
     }
    

    Note the blog also has an implementation of DownloadString which in turn uses WebClient which simplifies things. However the principles still apply if for some reason you must stick with HttpWebRequest. (Let me know if you are having trouble creating an AsyncOperation for this)

    You would then use this code like this:-

    int startId = GetSomeIDToStartWith();
    Foo myFoo = new Foo();
    
    myFoo.FetchItems(startId).Run((err) =>
    {
        // Clear IsBusy
        if (err == null)
        {
            // All items are now fetched continue doing stuff here.
    
        }
        else
        {
            // "Oops something bad happened" code here
        }
    }
    // Set IsBusy 
    

    Note that the call to Run is asynchronous, code execution will appear to jump past it before all the items are fetched. If the UI is useless to the user or even dangerous then you need to block it in a friendly way. The best way (IMO) to do this is with the BusyIndicator control from the toolkit, setting its IsBusy property after the call to Run and clearing it in the Run callback.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose you have following class: class ProcessController { public List<Process> Active { get {
Suppose I have the following class structure: public class State { public int Id
Suppose I have the following class: public class TestBase { public bool runMethod1 {
Suppose I have the following class: public class FixExpr { Expr<FixExpr> in; } Now
Suppose I have the following class: class Camera { public Camera( double exposure, double
Suppose i have the following class. public class Location { public Id { get;
Suppose I have the following (trivially simple) base class: public class Simple { public
Suppose I have the following class package { import flash.display.Stage; public class CustomObject {
Suppose we have the following class hierarchy: class Base { ... }; class Derived1
Suppose I have the following code: class some_class{}; some_class some_function() { return some_class(); }

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.