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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T22:33:23+00:00 2026-06-14T22:33:23+00:00

I want to chain Task s, then start the chain in parallel. This snippet

  • 0

I want to chain Tasks, then start the chain in parallel.
This snippet is just to illustrate my question:

        var taskOrig = new Task(() => { });
        var task = taskOrig;
        foreach (var msg in messages)
        {
            task=task.ContinueWith(t => Console.WriteLine(msg));
        }
        taskOrig.Start();

Everything works fine except a little perfectionist inside me doesn’t like having empty method executed first () => { }.

Is there any way to avoid it?

I do understand It’s barely affecting performance (unless you do it really often), but still. Performance matters in my case, so checking if task exists in every iteration is not the way to do it.

  • 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-14T22:33:24+00:00Added an answer on June 14, 2026 at 10:33 pm

    You could do this:

    Task task = Task.FromResult<object>(null);
    foreach (var msg in messages)
    {
        task = task.ContinueWith(t => Console.WriteLine(msg));
    }
    

    The previous solution won’t work in 4.0. In 4.0 you’d need to do the following instead:

    var tcs = new TaskCompletionSource<object>();
    Task task = tcs.Task;
    foreach (var msg in messages)
    {
        task = task.ContinueWith(t => Console.WriteLine(msg));
    }
    
    tcs.SetResult(null);
    

    (You can move SetResult to before the foreach loop if you prefer.)

    Technically it’s not the same as the continuations will start executing while you’re still adding more. That’s unlikely to be a problem though.

    Another option would be to use something like this:

    public static Task ForEachAsync<T>(IEnumerable<T> items, Action<T> action)
    {
        return Task.Factory.StartNew(() =>
        {
            foreach (T item in items)
            {
                action(item);
            }
        });
    }
    

    An example usage would be:

    ForEachAsync(messages, msg => Console.WriteLine(msg));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to create this Markov Chain in C#. I need to know if
I want to chain multiple Task s, so that when one ends the next
I want to chain methods in Javascript (using Node.js). However, I encountered this error:
I want to conditionally add an attribute using jQuery: var $this = this .append(...)
I want to generate a simple CMS signature using bouncycastle. This code works! Security.addProvider(new
Want the function to sort the table by HP but if duplicate HPs then
I want to chain the contents of two queries. Please notice I DON'T want
I want to split a bezier curve into a polygonal chain with n straight
I want to make an transparent chain-proxy using a redirect server and chain proxy
I want to chain async rest service calls and have single callback when they

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.