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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T22:35:38+00:00 2026-05-23T22:35:38+00:00

I have found a piece of code on the web and have modified it

  • 0

I have found a piece of code on the web and have modified it a bit to see how it works, but now I have a problem with ContinueWhenAll as it doesn’t wait for all tasks to be finished:

List<Task> tasks = new List<Task>();
for (int i = 0; i < 20; i++)
{
    int j = i;
    var compute = Task.Factory.StartNew(() => results.Add(DoSomething(j)));
    tasks.Add(compute);
}

I’m using this code to add all task to list. DoSomething function computes some results and adds them to BlockingCollection. I have another display function which writes all added results from BlockingCollection to the console.

I have used this code to wait for all tasks to complete, but it looks like it doesn’t wait for them as the program displays the standard "Press any key to continue" message just a few milliseconds after it was started. (It should take ~20 sec for program to complete)

Task.Factory.ContinueWhenAll(tasks.ToArray(), result => results.CompleteAdding());

However if I add Task.WaitAll(consume) to end of the program, program works fine:

var consume = Task.Factory.StartNew(() => display(results));
//results = BlockingCollection that I mentioned 

As far as I understand, the program will not have enough time to display all result from BlockingCollection but still it has more than enough time to display some while waiting for all tasks to complete.

Could someone explain me why Task.Factory.ContinueWhenAll doesn’t wait for all results to be computed and program comes to an end just like there is no that line of code in program (after few milliseconds)?

  • 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-23T22:35:38+00:00Added an answer on May 23, 2026 at 10:35 pm

    Task.Factory.ContinueWhenAll is not a blocking method; it will actually start a new task that will only function when all the provided tasks complete there execution, So it is normal to see a message just a few milliseconds after program has been started, because it will not block at your main waiting for the tasks to finish.
    From Microsoft Learn:

    Creates a continuation task that starts when a set of specified tasks has completed.

    Where the Task.WaitAll will block at the caller waiting for all of the provided Tasks to complete execution.

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

Sidebar

Related Questions

I have been working with a piece of code for youtube style URL's but
i'm playing around with a piece of code i found on the web and
I've been struggling with a piece of C# code and although I have found
I have this piece of code which is calling some functions in a web
I found this piece of code inside System.Web.ISAPIRuntime using Reflector public void DoGCCollect() {
I have found some in the Cappuccino website (vim, textmate and SubEthaEdit), but not
I have found some libraries or web services in PHP that does the job.
I have found a few libraries to edit MP3 tags (UltraID3Lib is great) but
I have found ASP.Net PageMethods very handy and easy to use, but I have
I found the following piece of code in apple guidelines: - (void)accelerometer:(UIAccelerometer*)accelerometer didAccelerate:(UIAcceleration*)acceleration {

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.