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

  • Home
  • SEARCH
  • 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 256403
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T22:00:40+00:00 2026-05-11T22:00:40+00:00

I have some simple code in which the main Thread is creating a new

  • 0

I have some simple code in which the main Thread is creating a new Task. The task in turn spawns multiple child tasks.
The main Thread does a ‘wait’ on the parent Task. I’m observing that I don’t get the same output across multiple runs of the program. In the code below I’m printing out the value of the iteration variable in each task but across different runs only subsets get printed out. I understand that the execution is not ordered but it would still have to be complete in that I’m expecting 100 numbers to get printed out(in no particular order).
Does calling Wait does not wait for all child tasks to complete?
I’m running VS2010 Team system Beta1

 static void Main(string[] args)
    {
        Console.WriteLine("Main executing on ThreadID:- " + Thread.CurrentThread.ManagedThreadId.ToString());

        var task = Task.Factory.StartNew(WriteNumbers);
        task.Wait();

    }

    private static void WriteNumbers()
    {
        Console.WriteLine("WriteNumbers executing on ThreadID:- " + Thread.CurrentThread.ManagedThreadId.ToString());
        for (int i = 0; i < 100; i++)
        {
            int localInt = i;
            Task.Factory.StartNew(() => 
                {
                   Write(localInt);
                }, TaskCreationOptions.DetachedFromParent);
        }
    }

    private static void Write(int i)
    {            
        Console.WriteLine("Worker Thread executing on ThreadID:-" + Thread.CurrentThread.ManagedThreadId.ToString() + " Value:" + i.ToString());
    }
  • 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-11T22:00:40+00:00Added an answer on May 11, 2026 at 10:00 pm

    Per MSDN, you need more waiting or other synchronization, and I quote:
    “””
    When you wait on a task, you implicitly wait on all children of that task, unless those children were created by using the DetachedFromParent option. Any detached children must be waited on separately.
    “””

    Here, main waits on parent, but since parent’s children were indeed created as detached, that does not make main implicitly wait on the children as well, so main (and therefore the whole process) can and often will terminate before some child task has had a chance to run.

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

Sidebar

Ask A Question

Stats

  • Questions 181k
  • Answers 181k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Try this, noting that the grammar of HTML is too… May 12, 2026 at 4:13 pm
  • Editorial Team
    Editorial Team added an answer You want to call vprintf() instead of printf() using the… May 12, 2026 at 4:13 pm
  • Editorial Team
    Editorial Team added an answer author_A = [['book_x',1,10],['book_y',2,20],['book_z',3,30]] author_B = [['book_s',5,10],['book_t',2,20],['book_z',3,30]] def present(A, B): Aset… May 12, 2026 at 4:13 pm

Related Questions

I am working on maintaining someone else's code that is using multithreading, via two
Here is a simplified version of my application showing what I'm doing. /* in
I have a simple UserControl for database paging, that uses a controller to perform
I saw in WCF they have the [OperationContract(IsOneWay = true)] attribute. But WCF seems

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.