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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T19:47:25+00:00 2026-06-01T19:47:25+00:00

I have a queue which have like 144 items in it. Each queue element

  • 0

I have a queue which have like 144 items in it. Each queue element is a request. I am trying to increase the performance of the processing by trying to run the code in parallel. I call performaction(request) method. Which instantiate an object and pass the request to it and perform an action. I can run this action parallely since these requests are independent and the action results of the requests are also independent of each other.
First I tried

foreach(var request in queue.Values)
    Parallel.Invoke(() =>PerformAction(request);

then I tried for the same foreach.

var task = new Task(() => PerformAction(request));
task.Start();

and outside the foreach I have

Task.WaitAll();

When the run the program the parallel.invoke took almost 3 second to complete while Task took almost 0.005s to complete.

I have the following questions:

  1. Why parallel invoke took more time than the Task? It seems the parallel invoke did not run in parallel mode as I wanted.
  2. When I am using Task, for every request I am creating a new Task, what if I have a request of 1000 request, will I endup creating 1000 threads or CPU will take care of creating threads as required?
  3. Task.WaitAll() out side will it wait for all the tasks to complete before the program terminates?
  • 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-01T19:47:27+00:00Added an answer on June 1, 2026 at 7:47 pm

    The problem in your code is that Task.WaitAll() doesn’t work as you intended. You’ve saved and started a task:

    var task = new Task(() => PerformAction(request));
    task.Start();
    

    But you haven’t kept a reference to each task to pass to WaitAll. You’re meant to save all the tasks in an array and then pass that array to WaitAll. So your program executes ‘fast’ with Tasks because it’s not waiting to finish.

    Your questions:

    1. See above for why Parallel.Invoke took more time – because it actually run. However, you’re also not using it correctly. Parallel.Invoke takes an array of actions (Actions[]) and tries to run them all in parallel. You’re calling it multiple times with a single action each time. I’m not even sure how your code compiles. I believe what you want is:

      Parallel.ForEach(queue.Values, request => PerformAction(request))
      
    2. You are creating a new task for each request, but that doesn’t map one to one with a thread. By default, tasks use the thread pool.

    3. Task.WaitAll(tasks) does wait for all tasks to finish. But it doesn’t if you don’t pass it anything. Again, this shouldn’t compile as you have it.

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

Sidebar

Related Questions

I have two tables which looks something like this Table Queue int ID; string
I have a deque type list (a queue) which I'd like to show and
I have a table in one of my databases which is a queue of
I have a message handler, which consumes from a JMS queue and that sends
using SubSonic3 SimpleRepository; I have a table used for an email queue which has
I have an in memory bounded queue in which multiple threads queue objects. Normally
I have a queue in which I can enqueue different threads, so I can
I would like to have one background thread which will copy the files through
We have a set of actions or Jobs which we'd like to happen one
Say I have a queue full of tasks which I need to submit to

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.