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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T14:34:54+00:00 2026-05-30T14:34:54+00:00

I use Task Parallel Library to start some tasks, like so: public static void

  • 0

I use Task Parallel Library to start some tasks, like so:

    public static void Main()
    {
        for (var i = 0; i < 10; i++)
        {
            var x = i;
            Task.Factory.StartNew(() => new WorkerClass(x).Do());
        }

        // (*) Here I'd like to wait for all tasks to finish
        Task.WaitAll(); 

        Console.WriteLine("Ready.");
        Console.ReadLine();
    }

The problem is that some tasks can create new tasks themselves. This is how WorkerClass looks like:

public class WorkerClass
{
    private static readonly NLog.Logger Log = NLog.LogManager.GetCurrentClassLogger();

    private readonly int _i;

    public WorkerClass(int i)
    {
        _i = i;
    }

    public void Do()
    {
        if (_i % 3 == 0)
            Task.Factory.StartNew(() => new WorkerClass(_i + 101).Do());

        Log.Info("Started {0}", _i);
        Thread.Sleep(2000);
        Log.Info("Done {0}", _i);
    }
}

For every value of i that’s a multiple of 3, a new Task is started.

I’d like to be able to wait until all tasks (including the ones created by other tasks) are finished.

Is there a clean/built-in way to do this (with or without TPL)?

  • 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-30T14:34:55+00:00Added an answer on May 30, 2026 at 2:34 pm

    Keep a reference to all top-level tasks and then just use WaitAll:

        var tasks = new Task[10];
        for (var i = 0; i < 10; i++)
        {
            var x = i;
            tasks[i] = Task.Factory.StartNew(() => new WorkerClass(x).Do());
        }
    
        Task.WaitAll( tasks );
    

    As for the child tasks, just make sure you attach them to the parent task. This means that the parent task will not go into a complete state until all child tasks are also finished.

        Task.Factory.StartNew(() => { }, TaskCreationOptions.AttachedToParent);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to use the parallel task library to kick off a number
I'm trying to use the Task-Parallel-Library to offload expensive ADO.NET database access from the
I want to use parallel arrays for a task, and before I start with
I'm trying to use scala parallel collections to implement some cpu-intensive task, I've wanted
Is there any way to use Task Parallel Library in multi computer scenarios ?
In my use of the Task Parallel Library in .net 4.0 I am wondering
I would like to use the task-list in Visual Studio but it really lacks
I'm trying to use the VssGet task of the MSBuild Community Tasks, and the
I just started playing with the Task Parallel Library, and ran into interesting issues;
I'm using the TPL ( Task Parallel Library ) in .NET 4.0. I want

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.