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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T23:52:01+00:00 2026-06-10T23:52:01+00:00

I am trying to get my head around Task.WaitAll(..) in the Task Parallel Library.

  • 0

I am trying to get my head around Task.WaitAll(..) in the Task Parallel Library.

I am running a test using the following code to try to simulate two tasks, one that will run longer (10 seconds) than the specified amount of time to wait for (10,, and one to run less (3 seconds) than the specified amount of time to wait for. The specified amount of time is 5 seconds. The code I am using is the following:

Task<int>[] tasks = new Task<int>[]
{
    Task.Factory.StartNew<int>(()=>
        {
            Thread.Sleep(10000);
            return 1;
        }),
    Task.Factory.StartNew<int>(()=>
        {
            Thread.Sleep(3000);
            return 2;
        })
};

TimeSpan timeSpan = new TimeSpan(0, 0, 5);
Task.WaitAll(tasks,timeSpan);

int[] results = new int[tasks.Length];
for(int i = 0; i < tasks.Length;i++)
{
    Console.WriteLine(tasks[i].Result);
}

As far as the timeSpan, I have also tried directly passing in 5000 instead of the timeSpan variable, but it is not working. I am getting the following output:

1
2

I would expect to only get the following output because the other thread is executing longer than the expected amount of time to wait.

1

Am I misunderstanding this, or doing this test wrong?

  • 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-10T23:52:02+00:00Added an answer on June 10, 2026 at 11:52 pm

    Do not use Thread.Sleep in tasks. The Task scheduler does not guarantee one thread per tasks and sleeping may affect other tasks (see Task is ignoring Thread.Sleep). Use Task.Delay() instead.

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

Sidebar

Related Questions

I'm trying to get my head around using CoffeeScript comprehensions as efficiently as possible.
Trying to get my head around backbone.js. This example is using Backbone Boilerplate and
am trying to get my head around the following: Have a small program am
I am trying to get my head around some of my predecessors code who,
Trying to get my head around using functions as arguments to methods. For a
I'm trying to get my head around solving the following problem. I have the
just trying to get my head around Rx I am using Rx to poll
Im trying to get my head around javascript inheritance and this code doesnt work
I am trying get my head around how you can run the following php:
I have been trying to get my head around C#'s new async/await and Task.Run

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.