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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T08:24:39+00:00 2026-06-02T08:24:39+00:00

As per the MSDN Documentation TaskFactory.StartNew it Creates and starts a Task. So, for

  • 0

As per the MSDN Documentation TaskFactory.StartNew it Creates and starts a Task. So, for the below code sample

class Program
{
    public static void Main()
    {
        var t =Task.Factory.StartNew(
                () => SomeLongRunningCalculation(10, Display)
            );
        var t1 = Task.Factory.StartNew(
                () => SomeLongRunningCalculation(20, Display)
            );
        Console.WriteLine("Invoked tasks");
        Task.WaitAll(t, t1);
        Console.ReadLine();
    }

    public static void Display(int value)
    {
        Console.WriteLine(value);
    }

    public static void SomeLongRunningCalculation(int j, Action<int> callBack)
    {
        Console.WriteLine("Invoking calculation for {0}", j);
        System.Threading.Thread.Sleep(1000);
        if (callBack != null)
        {
            callBack(j + 1);
        }
    }
}      

My expected output was

Invoking calculation for 10
Invoking calculation for 20
Invoked tasks
11 
21

But, it is displaying

Invoked tasks
Invoking calculation for 20
Invoking calculation for 10
21
11

I would like to learn

  1. Why the tasks are not running immediately after StartNew?
  2. What should I do, to get the output in the expected format?
  • 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-02T08:24:41+00:00Added an answer on June 2, 2026 at 8:24 am

    This is a very likely outcome on a machine with a single core cpu. Or possible on one with a multi-core cpu and it is also busy doing something else.

    Creating a task or a thread only sets up a logical operating system structure that allows code to run. The operating system scheduler does not immediately start executing it if the cores are busy, the thread has to compete with all the other threads that are running on the machine. A typical Windows session has a thousand or so. 64 times per second, the kernel generates an interrupt and the scheduler re-evaluates what’s going on to see if another thread should get a turn. Any threads that are not blocking (waiting for some other thread to get a job done like read a file or network packet) are eligible to run and the scheduler picks the one with with the highest priority. Some additional code in the scheduler tinkers with the priority values to ensure that all threads get a chance.

    Chance is the key word here. Thread scheduling is non-deterministic.

    Note how I never said anything about the Thread or Task or ThreadPool class. They don’t have the power to do much of anything about the way the operating system schedules threads. All that’s possible is prevent a thread from running, the job of the thread pool scheduler.

    Priority matters, you can tinker with the Thread.Priority or Task.Priority property to affect outcome. But no slamdunk, the operating system scheduler constantly adjusts a threads priority from the base priority you set with that property. You cannot prevent a thread from ever running by having another one with a higher priority for example.

    Hoping that threads run in a predictable order causes the worst kind of bug, a threading race bug. Second worst is deadlock. They are extremely hard to debug because they depend on timing and available machine resources and load. You can only ensure that you get a particular order by writing code that explicitly takes care of it. Which you do by using a threading primitive like Mutex or the lock keyword. Notable too is that when you try to add such code to your snippet then you’ll end up with a program that no longer has concurrency. Or in other words, you’ll end up not having any use for a Task anymore. A thread or task will only be useful if you can afford for it to run at unpredictable times.

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

Sidebar

Related Questions

Per the code below, I am getting the following message. I am fairly certain
Per the MSDN documentation , the following syntax is used: // A read-write instance
As per the MSDN documentation, while writing recursive function, The use of the accumulator
I am creating a C# class as per: http://msdn.microsoft.com/en-us/library/x6h10s6x.aspx however I want my own
Per MSDN: A static constructor does not take access modifiers or have parameters. A
As per the MSDN documentation with the Office 2007 system, office Addins are registered
As per this MSDN article: W3C Extended Log File Format (IIS 6.0) It says
http://msdn.microsoft.com/en-us/library/dd997415.aspx Per the article referenced above I am trying to handle exceptions in a
Using Visual Studio 2005 As per the title; MSDN and google can't tell me,
Per the documentation , String.Format will throw a FormatException if either (A) the format

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.