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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T17:55:44+00:00 2026-06-15T17:55:44+00:00

First look at this simple method: Public Iterator Function GetLongRunningTasks(count As Long) As IEnumerable(Of

  • 0

First look at this simple method:

Public Iterator Function GetLongRunningTasks(count As Long) As IEnumerable(Of Task)
    For i = 1 To count
        Yield Task.Delay(3000)
    Next
End Function

This method returns the specified amount of tasks, each one of which completes 3 seconds after it starts. Let’s call it a simulation of a network API call on an extremely bad network connection (not that it matters).

My problem is that a simple iteration will start the tasks one at a time, so the 3000ms delay happens in between each iteration.

    For Each t In GetLongRunningTasks(50)
        Await t
    Next ' this takes ~150 seconds to complete (50x3000ms)

What I want to do is start all 50 tasks at once, then afterwards enter the foreach-loop. Preferably sticking to the example above, what is the proper way to do this?

EDIT

As suggested by Stephen, one solution is to iterate over GetLongRunningTasks(50).ToList(). Maybe it’s just me but I don’t think it’s at all obvious why ToList is used when I read over the code.

I wonder if the following snippet is exactly the same?

    Dim tasks As New List(Of Task)
    tasks.AddRange(GetLongRunningTasks(50))

    For Each t In tasks
        Await t
    Next
  • 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-15T17:55:45+00:00Added an answer on June 15, 2026 at 5:55 pm

    Just to add some explanation to Stephen’s answer: GetLongRunningTasks() returns a lazy iterator, which creates the Tasks only when you iterate it. In your original code, each iteration creates a single Task, then waits for it to complete and only then another iteration starts, which starts another Task.

    So, what you need it to first iterate over the whole collection to start all the Tasks and wait for them to complete only when you have them all. Stephen’s ToList() suggestion does exactly that, and your AddRange() would do the same.

    If it still isn’t clear to you, maybe one more way to do the same would help:

    Dim tasks As New List(Of Task)
    For Each t in GetLongRunningTasks(50)
        tasks.Add(t)
    Next
    
    For Each t In tasks
        Await t
    Next
    

    Also, starting a large number of IO-bound Tasks most likely isn’t the most efficient option, running them with a limited degree of parallelism is. To do that, you could use SemaphoreSlim‘s WaitAsnyc(), or use ActionBlock with MaxDegreeOfParallelism set from TPL Dataflow.

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

Sidebar

Related Questions

First look at this url: https://stackoverflow.com/questions/tagged/xoxoxo/ This directory does not exists but somehow stackoverflow
First look at this code: class Program { static void Main(string[] args) { var
have a problem. At first look at this HTML <div id=map style=background-image: url(map.png); width:
first take a look on this picture from localScope app : i have 2
Ok let's first look at a screen shot. This is a screen shot of
I have tried this example http://www.bennadel.com/blog/1056-ColdFusion-CFPOP-My-First-Look.htm , but it retrieve emails from POP server.
look at this fiddle: http://jsfiddle.net/ugxNK/ I want that the first list element is in
First of all I did gave a look at this one. But I didn't
Take a look at this screenshoot first: That white box is ON the orange
Please take a look at this screenshot first: I would like to move the

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.