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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T03:58:00+00:00 2026-05-18T03:58:00+00:00

I have a requirement to run a set of heavy functions asynchronously at sametime

  • 0

I have a requirement to run a set of heavy functions asynchronously at sametime and populate the results in a list. Here is the pseudo code for this :

List<TResult> results = new List<TResults>();
List<Func<T, TResult>> tasks = PopulateTasks();

foreach(var task in tasks)
{
    // Run Logic in question
    1. Run each task asynchronously/parallely
    2. Put the results in the results list upon each task completion
}

Console.WriteLine("All tasks completed and results populated");

I need the logic inside the foreach bock. Can you guys plz help me?

I have some constraint : The solution must be .net 3.5 compliant (not .net 4, but a .net 4 alternative solution would be appreciated for my knowledge purpose)

Thanks in advance.

  • 1 1 Answer
  • 2 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-18T03:58:01+00:00Added an answer on May 18, 2026 at 3:58 am

    A simple 3.5 implementation could look like this

    List<TResult> results = new List<TResults>();
    List<Func<T, TResult>> tasks = PopulateTasks();
    
    ManualResetEvent waitHandle = new ManualResetEvent(false);
    void RunTasks()
    {
        int i = 0;
        foreach(var task in tasks)
        {
            int captured = i++;
            ThreadPool.QueueUserWorkItem(state => RunTask(task, captured))
        }
    
        waitHandle.WaitOne();
    
        Console.WriteLine("All tasks completed and results populated");
    }
    
    private int counter;
    private readonly object listLock = new object();
    void RunTask(Func<T, TResult> task, int index)
    {
        var res = task(...); //You haven't specified where the parameter comes from
        lock (listLock )
        {
           results[index] = res;
        }
        if (InterLocked.Increment(ref counter) == tasks.Count)
            waitHandle.Set();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

so I have this requirement to kick off Activities provided to me at run-time.
On the project that I'm currently working I have a requirement to run a
I have a requirement such that my program is suppose to run for a
I have requirement to disable copy/paste/cut operations on a textbox. For this purpose I
I have requirement to get Facebook friends list with their images. How can I
I have a set of classes that I've written in C++ for a code
I have a requirement of tracking changes to some set of variables in a
I have a requirement to flatten a resultset in an SSIS package - this
Hi I have requirement to create ,compile and load java classes run time. Using
I have a requirement where in I have a set of jobs to be

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.