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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T05:01:12+00:00 2026-05-14T05:01:12+00:00

I need to retrieve multiple objects from an external system. The external system supports

  • 0

I need to retrieve multiple objects from an external system. The external system supports multiple simultaneous requests (i.e. threads), but it is possible to flood the external system – therefore I want to be able to retrieve multiple objects asynchronously, but I want to be able to throttle the number of simultaneous async requests. i.e. I need to retrieve 100 items, but don’t want to be retrieving more than 25 of them at once. When each request of the 25 completes, I want to trigger another retrieval, and once they are all complete I want to return all of the results in the order they were requested (i.e. there is no point returning the results until the entire call is returned). Are there any recommended patterns for this sort of thing?

Would something like this be appropriate (pseudocode, obviously)?

  private List<externalSystemObjects> returnedObjects = new List<externalSystemObjects>;

  public List<externalSystemObjects> GetObjects(List<string> ids)
  {
      int callCount = 0;
      int maxCallCount = 25;
      WaitHandle[] handles;

      foreach(id in itemIds to get)
      {
          if(callCount < maxCallCount)
          {
               WaitHandle handle = executeCall(id, callback);
               addWaitHandleToWaitArray(handle)
          }
      else
      {
           int returnedCallId = WaitHandle.WaitAny(handles);
           removeReturnedCallFromWaitHandles(handles);
      }
   }

   WaitHandle.WaitAll(handles);

   return returnedObjects;
   }

   public void callback(object result)
   {
         returnedObjects.Add(result);
   }
  • 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-14T05:01:12+00:00Added an answer on May 14, 2026 at 5:01 am

    Consider the list of items to process as a queue from which 25 processing threads dequeue tasks, process a task, add the result then repeat until the queue is empty:

     class Program
      {
        class State
        {
          public EventWaitHandle Done;
          public int runningThreads;
          public List<string> itemsToProcess;
          public List<string> itemsResponses;
        }
    
        static void Main(string[] args)
        {
          State state = new State();
    
          state.itemsResponses = new List<string>(1000);
          state.itemsToProcess = new List<string>(1000);
          for (int i = 0; i < 1000; ++i)
          {
            state.itemsToProcess.Add(String.Format("Request {0}", i));
          }
    
          state.runningThreads = 25;
          state.Done = new AutoResetEvent(false);
    
          for (int i = 0; i < 25; ++i)
          {
            Thread t =new Thread(new ParameterizedThreadStart(Processing));
            t.Start(state);
          }
    
          state.Done.WaitOne();
    
          foreach (string s in state.itemsResponses)
          {
            Console.WriteLine("{0}", s);
          }
        }
    
        private static void Processing(object param)
        {
          Debug.Assert(param is State);
          State state = param as State;
    
          try
          {
            do
            {
              string item = null;
              lock (state.itemsToProcess)
              {
                if (state.itemsToProcess.Count > 0)
                {
                  item = state.itemsToProcess[0];
                  state.itemsToProcess.RemoveAt(0);
                }
              }
              if (null == item)
              {
                break;
              }
              // Simulate some processing
              Thread.Sleep(10);
              string response = String.Format("Response for {0} on thread: {1}", item, Thread.CurrentThread.ManagedThreadId);
              lock (state.itemsResponses)
              {
                state.itemsResponses.Add(response);
              }
            } while (true);
    
          }
          catch (Exception)
          {
            // ...
          }
          finally
          {
            int threadsLeft = Interlocked.Decrement(ref state.runningThreads);
            if (0 == threadsLeft)
            {
              state.Done.Set();
            }
          }
        }
      }
    

    You can do the same using asynchronous callbacks, there is no need to use threads.

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

Sidebar

Ask A Question

Stats

  • Questions 371k
  • Answers 371k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Yes, if you get your form working great without Ajax,… May 14, 2026 at 6:55 pm
  • Editorial Team
    Editorial Team added an answer Hours later.... My solution As noted above, I finally found… May 14, 2026 at 6:55 pm
  • Editorial Team
    Editorial Team added an answer Is this what you are looking for ? QTimer::elapsed() uses… May 14, 2026 at 6:55 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.