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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T07:18:18+00:00 2026-06-12T07:18:18+00:00

We have a webservice function that runs a list of tasks in the background.

  • 0

We have a webservice function that runs a list of tasks in the background. When all tasks are complete, it returns. I’ve revisited the function for C# 5. I’ve managed to get the following snippet working:

var result = new List<int>();
var tasks = new List<Task<int>>();

foreach (var row in rowlist)
    tasks.Add(Task.Factory.StartNew(() => DoWork(row)));
foreach (var task in tasks)
    result.Add(task.Result);

The task.Result waits for the individual task to be done. I’ve tested this by adding a three second delay to the DoWork method and verifying that it finishes in less than 5 seconds, for a list of 10 tasks.

Now I’ve tried to rewrite the function using the new async / await syntax. But I can’t seem to get it to work. If it compiles, it runs synchronously, as verified by adding a wait.

Any ideas on how you can rewrite the above snippet using async / await ?

  • 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-12T07:18:19+00:00Added an answer on June 12, 2026 at 7:18 am

    I would write this as:

    var tasks = rowlist.Select(row => Task.Run(() => DoWork(row)));
    
    var results = (await Task.WhenAll(tasks)).ToList();
    

    Basically, you still create the tasks the way you were previously (without using await), as you want them to all start immediately. You can then wait for all of them to complete asynchronously (via await Task.WhenAll), then pull out the results at once (since you know they’re all done).

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

Sidebar

Related Questions

I have a webservice function that returns something like the following: New Orleans, Louisiana
I have a webservice with a function that returns a type (foo). If I
I have an ASP.NET webservice method that returns a generics list (List'<'Construct>) serialized as
I have created a function in PHP that calls a webservice and parses through
I have a WebService in Java (Using Apache Axis) that get's the id of
I have a login function that authorizes against a remote database over xml webservice.
I have a webservice that exposes a function called ADD(int a, int b) and
I have a powershell process that runs on demand which collects all Request History
I have an third part dll that have a function that returns a string.
I have a webservice that returns for example a DateTime object: DepartureDate. I use

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.