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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T23:01:07+00:00 2026-05-25T23:01:07+00:00

ReWrite: I have stripped the question down to contain the example code instead of

  • 0

ReWrite: I have stripped the question down to contain the example code instead of a link to it and the results and question as the surrounding information seemed to be causing confusion.

Question: Why do tasks seem to start with out being started?

This is a piece of sample code from the Microsoft Website on tasks. It is described as creating an unstarted task, creating a second started task and waiting for it to complete, then starting the first task and finally starting a third task synchronously in the main thread.

Here is an uncommented version of the sample code:

using System;
using System.Threading;
using System.Threading.Tasks;

class StartNewDemo
{
    static void Main()
    {
        Action<object> action = (object obj) =>
        {
            Console.WriteLine("Task={0}, obj={1}, Thread={2}", Task.CurrentId, obj.ToString(), Thread.CurrentThread.ManagedThreadId);
        };

        Task t1 = new Task(action, "alpha");

        Task t2 = Task.Factory.StartNew(action, "beta");

        t2.Wait();

        t1.Start();

        Console.WriteLine("t1 has been launched. (Main Thread={0})", Thread.CurrentThread.ManagedThreadId);

        t1.Wait();

        Task t3 = new Task(action, "gamma");

        t3.RunSynchronously();

        t3.Wait();
    }
}

Here are the results:

Task=1, obj=beta, Thread=3
t1 has been launched. (Main Thread=1)
Task=2, obj=alpha, Thread=3
Task=3, obj=gamma, Thread=1

From the description task 1, which is Beta, should be ran only after task 2 has completed so the main thread can progress. This does not seem to be the case from this output. I also ran a second test run where I put a display of the tick counts into the action object and received this, confirming that Task1 is indeed starting before Task2 is completed.

Task=1, obj=beta, Thread=3 (634529151744201906)
t1 has been launched. (Main Thread=1)
Task=2, obj=alpha, Thread=3 (634529151744221908)
Task=3, obj=gamma, Thread=1 (634529151744221908)

I am not trying to do anything fancy here, I am simply looking for an explanation as to why Task1 (obj=beta) is apparently executing before it is given the instruction to do so.

  • 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-25T23:01:08+00:00Added an answer on May 25, 2026 at 11:01 pm
    Task=1, obj=beta, Thread=3
    

    Task=1 is slightly misleading here, as this is actually t2 in the sample code. You can see this because ‘beta’ is present. It is started and completed before running t1.

    t1 has been launched. (Main Thread=1)
    Task=2, obj=alpha, Thread=3
    

    This is t1 (with Id 2).

    Task=3, obj=gamma, Thread=1
    

    This is t3.

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

Sidebar

Related Questions

I have to rewrite code that references a 2-dimensional array using pointers instead of
I have to rewrite windows-code into crossplatform view. Here is the example: std::unordered_set<Type>::iterator it
I have to rewrite some old code and I'm wondering the best way to
I'm about to have to rewrite some rather old code using SQL Server's BULK
Quick one. I'm using mod rewrite and have most replacements in place: empty space
I have this rewrite rule RewriteEngine On RewriteBase /location/ RewriteRule ^(.+)/?$ index.php?franchise=$1 Which is
I have a rewrite rule of the following form: RewriteRule ^foo/([a-zA-Z0-9]+)$ foo.php?arg=$1 [qsa,nc] It
Say I have a rewrite that needs to pass the url to a PHP
I have the following rewrite rule rewrite ^/ab(.*)/(.*)$ /repo/ab$1/rtest/$2 break; When the request file
I have a rewrite that nginx calls like so: location ~* (css)$ { rewrite

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.