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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T15:06:24+00:00 2026-06-10T15:06:24+00:00

In Main method of a console application: Do().ContinueWith(t => Do()) .ContinueWith(t => Do()) .ContinueWith(t

  • 0

In Main method of a console application:

Do().ContinueWith(t => Do())
    .ContinueWith(t => Do())
    .ContinueWith(t => Do());

Do is a method that returns Task:

var source = new CancellationTokenSource();
var token = source.Token;
return Task.Factory.StartNew(() =>
{
    Console.WriteLine("Inside " + _Counter);
    token.WaitHandle.WaitOne(1000);
    Console.WriteLine(_Counter++ + " is done");
}, token);

And _Counter is an integer field:

private static int _Counter = 1;

When I run, the result is this:

Inside 1
1 is done
Inside 2
Inside 2
Inside 2
2 is done
3 is done
4 is done

So let’s assume I have a Task called t and an Action<Task> called a.
If I call t.ContinueWith(a), a should be called after t completes, right? And when a runs, that should mean whatever delegate t calls has ended.

What causes this result? Am I not getting something obvious here?
What I use:

  • Windows 8 RTM
  • .NET Framework 4.5
  • 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-10T15:06:25+00:00Added an answer on June 10, 2026 at 3:06 pm

    If I call t.ContinueWith(a), a should be called after t completes, right?

    Sure. But since your Do function creates a new task, it completes immediately, thus starting the next Do. Remove the task creation from Do (leaving only the Console.WriteLine stuff) and it should work as expected.

        static void Do()
        {
            Console.WriteLine("Inside " + _Counter);
            Thread.Sleep(1000);
            Console.WriteLine(_Counter + " is done");
        }
    
        static void Main(string[] args)
        {
            Task.Factory.StartNew(Do)
                .ContinueWith(t => Do())
                .ContinueWith(t => Do())
                .ContinueWith(t => Do());
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know that for Console/Windows application in C#, Main method is entry point to
Okay, so I've got a .NET console application with it's Main method, contained in
I have created a console application in C# and there is main method (static)
I was wandering why when I create console application and 'transform' the main method
Consider the console application below, featuring a method with a generic catch handler that
My main method: public static void main(String[] args) { Scanner input = new Scanner(System.in);
I saw a Java example that had a main method labeled as synchronized, calling
My application uses a Spring DefaultMessageListenerContainer to process incoming messages. The main method of
I have the following console application written in VB.NET: Sub Main() Dim ie As
I am writing a simple console application in Objective-C but I have heard that

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.