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 6328625
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T17:29:35+00:00 2026-05-24T17:29:35+00:00

So I’ve got this code to drill down into a heirarchy of XML documents

  • 0

So I’ve got this code to drill down into a heirarchy of XML documents from a REST api. I posted earlier to get advice on how to make it recursive, then I went ahead and made it parralel.

First, I was SHOCKED by how fast it ran – it pulled down 318 XML docs in just under 12 seconds, compared to well over 10 minutes single threaded – I really didn’t expect to gain that much. Is there some catch to this, because it seems too good to be true?

Second, I suspect this code is implementing a common pattern but possibly in a non “idiomatic” way. I have kind of a “producer-consumer queue” happening, with two separate locking objects. Is there a more standard way I could have done this?

Code.

        public class ResourceGetter
        {
            public ResourceGetter(ILogger logger, string url)
            {
                this.logger = logger;
                this.rootURL = url;
            }
            public List<XDocument> GetResources()
            {
                GetResources(rootURL);
                while (NumTasks() > 0) RemoveTask().Wait();
                return resources;
            }
            void GetResources(string url)
            {
                logger.Log("Getting resources at " + url);
                AddTask(Task.Factory.StartNew(new Action(() =>
                {
                    var doc = XDocument.Parse(GetXml(url));
                    if (deserializer.CanDeserialize(doc.CreateReader()))
                    {
                        var rl = (resourceList)deserializer.Deserialize(doc.CreateReader());
                        foreach (var item in rl.resourceURL)
                        {
                            GetResources(url + item.location);
                        }
                    }
                    else
                    {
                        logger.Log("Got resource for " + url);
                        AddResrouce(doc);
                    }
                })));
            }
            object resourceLock = new object();
            List<XDocument> resources = new List<XDocument>();
            void AddResrouce(XDocument doc)
            {
                lock (resourceLock)
                {
                    logger.Log("add resource");
                    resources.Add(doc);
                }
            }
            object taskLock = new object();
            Queue<Task> tasks = new Queue<Task>();
            void AddTask(Task task)
            {
                lock (taskLock)
                {
                    tasks.Enqueue(task);
                }
            }
            Task RemoveTask()
            {

                lock (taskLock)
                {
                    return tasks.Dequeue();
                }
            }
            int NumTasks()
            {
                lock (taskLock)
                {
                    logger.Log(tasks.Count + " tasks left");
                    return tasks.Count;
                }
            }
            ILogger logger;
            XmlSerializer deserializer = new XmlSerializer(typeof(resourceList));
            readonly string rootURL;
        }
  • 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-24T17:29:36+00:00Added an answer on May 24, 2026 at 5:29 pm

    Just offhand, I wouldn’t bother with the code for managing the task list, all the locking, and the NumTasks() method. It would be simpler to just use a CountdownEvent, which is threadsafe to begin with. Just increment it when you create a new task, and decrement it when a task finishes, kind of like you are doing now but without the locking.

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
I am currently running into a problem where an element is coming back from
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
Does anyone know how can I replace this 2 symbol below from the string
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into

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.