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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T08:51:40+00:00 2026-06-07T08:51:40+00:00

So I have a business object that supports a Save-method which does some IO

  • 0

So I have a business object that supports a “Save”-method which does some IO to some device. I then have a list of those objects which I want to save asynchronously in a batch. My code now looks like this:

    public async Task Save()
    {
        foreach (var element in Elements)
        {
            await element.Save();
        }
    }    

Now this results in n number of awaits, and I know that each await causes a bit of CPU overhead. I want to eliminate this and only have one single await. How do I refactor to achieve this?

  • 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-07T08:51:43+00:00Added an answer on June 7, 2026 at 8:51 am

    Well, you could call Save() on everything and then await all of them finishing using Task.WhenAll:

    public async Task Save()
    {
        await Task.WhenAll(Elements.Select(x => x.Save());
    } 
    

    or if you really don’t do anything else, just:

    public Task Save()
    {
        return Task.WhenAll(Elements.Select(x => x.Save());
    } 
    

    EDIT: If you want to do them serially, use the code you’ve already got. It’s worth noting that the way async/await has been designed, awaiting a call which actually completes synchronously (e.g. a cache hit, or in your case dirty checking) is really cheap. It doesn’t need to do any task scheduling, create a continuation or anything like that. You say:

    If I have a list of 10000 objects, and only 1 is dirty, I will end up with 9999 unnecessary async-awaits, which I suspect will be significant.

    As ever, suspicions about performance bottlenecks are pretty much meaningless – what’s important is evidence about performance bottlenecks. Have you tried the existing code and measured the cost? If not, I strongly suggest you do that before changing anything.

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

Sidebar

Related Questions

I have a service object that is responsible for some business logic validation. What
So, I have a ListBox which is bound to a list of business objects,
Assume I have a business object that has some properties that should be read-only.
I have a list of business objects that I want to display in a
I have a JUnit4 test which extends AbstractTransactionalJUnit4SpringContextTests. I have a business object that
I have a class that represents some business object, such as this: Public Class
I have a business object that compiles into a DLL that handles all calculations
Lets say that you have a business object whose current state implies that there
I have the following sort descriptors that sort an array of my business objects,
I'm stumped here. I have an observable collection that holds business objects. I have

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.