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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T04:14:04+00:00 2026-06-15T04:14:04+00:00

I have an application that contains a bunch of sections, each containing data that

  • 0

I have an application that contains a bunch of sections, each containing data that takes significant time to calculate. There are a few approaches I can see to this:

  1. Simple solution 1: Calculate the data in all the sections on application startup. This means application startup will be slow.

  2. Simple solution 2: Calculate the data in each section as the user loads it. This means that loading each section will be slow (which is bad, because each section has to do significant rendering calculation already, so they’re already a bit slow.

  3. Because performance is critical for this application, neither of these solutions is ideal. So this brings me to a less-simple solution: on app startup, start calculating section data on a background thread. The problem with this solution is that I don’t have a good way of predicting which section the user will load first. So if they load the last section to load on the background thread, they’ll end up waiting longer than if I had just gone with solution 2.

  4. So this brings me to solution: on app startup, start calculating section data on a background thread, but when the user loads a section, pause the background thread and start the calculation for the section the user requested. Unless the calculation is already started, in which case you let it finish.

I’m not sure how to implement solution 4. I think the simplest solution is probably to use a priority queue and just up the priority of a task when it’s loaded, but I’m not sure how to implement this in C#. Is this a reasonable approach? What good libraries are there for priority queues in C#?

  • 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-15T04:14:05+00:00Added an answer on June 15, 2026 at 4:14 am

    So we’ll start with an array (or some other data structure if you want) of Task<Section> objects.

    private Task<Section>[] sections = new Task<Section>[5];
    

    You should ensure that the collection is created and actually define the tasks at the very start of your application (not in the background).

    for (int i = 0; i < sections.Length; i++)
    {
        int sectionNumber = i; //copy for closure
        Task<Section> next = new Task<Section>(() => CreateSection(sectionNumber));
        //note the task isn't started.
        sections[i] = next;
    }
    

    Then you can start the background task to actually process the tasks one at a time:

    Task.Run(() =>
    {
        for (int i = 0; i < sections.Length; i++)
        {
            EnsureStarted(sections[i]);
            sections[i].Wait();
        }
    });
    

    You could use a Parallel.For if you want to have several threads working on items.

    That method uses this helper method:

    public void EnsureStarted(Task task)
    {
        if (task.Status == TaskStatus.Created)
        {
            task.Start();
        }
    }
    

    Then to actually get a finished section (starting it if needed):

    public Section GetFinishedSection(int sectionNumber)
    {
        EnsureStarted(sections[sectionNumber]);
        return sections[sectionNumber].Result;
    }
    

    If you need a non-blocking version of that (possibly to await it) you can use this:

    public Task<Section> EnsureSectionComplete(int sectionNumber)
    {
        EnsureStarted(sections[sectionNumber]);
        return sections[sectionNumber];
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a web application that contains a bunch of classes in the App_Code
I have an application that contains many controls on a panel, each with its
I have this senario. We have an application server that contains a few web
I am building a Rails application that contains Developer s who have Application s.
I have an application that contains a VC++ project (along with C# projects). Previously,
I have a winform application that contains a c# webbrowser control.Webbrower control load a
I have a .NET application that contains a checkbox (System.Windows.Forms.Checkbox). This component (WindowsForms10.BUTTON.app.0.378734a1) is
I have a silverlight application that contains Bing map. I want when the user
I have a MVVM application that contains multiple views with some complex IsReadOnly rules
I have a monodroid application that uses a .net library that contains normal .resx

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.