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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T01:56:36+00:00 2026-06-05T01:56:36+00:00

// let’s say there is a list of 1000+ URLs string[] urls = {

  • 0
// let's say there is a list of 1000+ URLs
string[] urls = { "http://google.com", "http://yahoo.com", ... };

// now let's send HTTP requests to each of these URLs in parallel
urls.AsParallel().ForAll(async (url) => {
    var client = new HttpClient();
    var html = await client.GetStringAsync(url);
});

Here is the problem, it starts 1000+ simultaneous web requests. Is there an easy way to limit the concurrent amount of these async http requests? So that no more than 20 web pages are downloaded at any given time. How to do it in the most efficient manner?

  • 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-05T01:56:38+00:00Added an answer on June 5, 2026 at 1:56 am

    You can definitely do this in the latest versions of async for .NET, using .NET 4.5 Beta. The previous post from ‘usr’ points to a good article written by Stephen Toub, but the less announced news is that the async semaphore actually made it into the Beta release of .NET 4.5

    If you look at our beloved SemaphoreSlim class (which you should be using since it’s more performant than the original Semaphore), it now boasts the WaitAsync(...) series of overloads, with all of the expected arguments – timeout intervals, cancellation tokens, all of your usual scheduling friends 🙂

    Stephen’s also written a more recent blog post about the new .NET 4.5 goodies that came out with beta see What’s New for Parallelism in .NET 4.5 Beta.

    Last, here’s some sample code about how to use SemaphoreSlim for async method throttling:

    public async Task MyOuterMethod()
    {
        // let's say there is a list of 1000+ URLs
        var urls = { "http://google.com", "http://yahoo.com", ... };
    
        // now let's send HTTP requests to each of these URLs in parallel
        var allTasks = new List<Task>();
        var throttler = new SemaphoreSlim(initialCount: 20);
        foreach (var url in urls)
        {
            // do an async wait until we can schedule again
            await throttler.WaitAsync();
    
            // using Task.Run(...) to run the lambda in its own parallel
            // flow on the threadpool
            allTasks.Add(
                Task.Run(async () =>
                {
                    try
                    {
                        var client = new HttpClient();
                        var html = await client.GetStringAsync(url);
                    }
                    finally
                    {
                        throttler.Release();
                    }
                }));
        }
    
        // won't get here until all urls have been put into tasks
        await Task.WhenAll(allTasks);
    
        // won't get here until all tasks have completed in some way
        // (either success or exception)
    }
    

    Last, but probably a worthy mention is a solution that uses TPL-based scheduling. You can create delegate-bound tasks on the TPL that have not yet been started, and allow for a custom task scheduler to limit the concurrency. In fact, there’s an MSDN sample for it here:

    See also TaskScheduler .

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

Sidebar

Related Questions

Let say I've this URL: http://example.com/image-title/987654/ I want to insert download to the part
let's say I have a select list as follows: <select name='languages'> <option value='german'>German</option> <option
Let's say I have the following classes : public class MyProductCode { private String
Let's say I got a Map<String, String> and I wanna remove all the entries
Let's say there is a graph and some set of functions like: create-node ::
Let's say I wanted to remove vowels from HTML: <a href=foo>Hello there!</a>Hi! becomes <a
Let's say I have an abstract parent class called shape, and that there are
Let's say I have the string: hello world; some random text; foo; How could
Let's say I have table with column 'URL' whrere I store urls like this
Let's say we have a simple function defined in a pseudo language. List<Numbers> SortNumbers(List<Numbers>

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.