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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T17:05:36+00:00 2026-05-26T17:05:36+00:00

Let’s say I have two sequences returning integers 1 to 5. The first returns

  • 0

Let’s say I have two sequences returning integers 1 to 5.

The first returns 1, 2 and 3 very fast, but 4 and 5 take 200ms each.

public static IEnumerable<int> FastFirst()
{
    for (int i = 1; i < 6; i++)
    {
        if (i > 3) Thread.Sleep(200);
        yield return i;
    }
}

The second returns 1, 2 and 3 with a 200ms delay, but 4 and 5 are returned fast.

public static IEnumerable<int> SlowFirst()
{
    for (int i = 1; i < 6; i++)
    {
        if (i < 4) Thread.Sleep(200);
        yield return i;
    }
}

Unioning both these sequences give me just numbers 1 to 5.

FastFirst().Union(SlowFirst());

I cannot guarantee which of the two methods has delays at what point, so the order of the execution cannot guarantee a solution for me. Therefore, I would like to parallelise the union, in order to minimise the (artifical) delay in my example.

A real-world scenario: I have a cache that returns some entities, and a datasource that returns all entities. I’d like to be able to return an iterator from a method that internally parallelises the request to both the cache and the datasource so that the cached results yield as fast as possible.

Note 1: I realise this is still wasting CPU cycles; I’m not asking how can I prevent the sequences from iterating over their slow elements, just how I can union them as fast as possible.

Update 1: I’ve tailored achitaka-san’s great response to accept multiple producers, and to use ContinueWhenAll to set the BlockingCollection’s CompleteAdding just the once. I just put it here since it would get lost in the lack of comments formatting. Any further feedback would be great!

public static IEnumerable<TResult> SelectAsync<TResult>(
    params IEnumerable<TResult>[] producer)
{
    var resultsQueue = new BlockingCollection<TResult>();

    var taskList = new HashSet<Task>();
    foreach (var result in producer)
    {
        taskList.Add(
            Task.Factory.StartNew(
                () =>
                    {
                        foreach (var product in result)
                        {
                            resultsQueue.Add(product);
                        }
                    }));
    }

    Task.Factory.ContinueWhenAll(taskList.ToArray(), x => resultsQueue.CompleteAdding());

    return resultsQueue.GetConsumingEnumerable();
}
  • 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-05-26T17:05:37+00:00Added an answer on May 26, 2026 at 5:05 pm

    Take a look at this.
    The first method just returns everything in order results come.
    The second checks uniqueness. If you chain them you will get the result you want I think.

    public static class Class1
    {
        public static IEnumerable<TResult> SelectAsync<TResult>(
            IEnumerable<TResult> producer1,
            IEnumerable<TResult> producer2,
            int capacity)
        {
            var resultsQueue = new BlockingCollection<TResult>(capacity);
            var producer1Done = false;
            var producer2Done = false;
    
            Task.Factory.StartNew(() =>
            {
                foreach (var product in producer1)
                {
                    resultsQueue.Add(product);
                }
                producer1Done = true;
                if (producer1Done && producer2Done) { resultsQueue.CompleteAdding(); }
            });
    
            Task.Factory.StartNew(() =>
            {
                foreach (var product in producer2)
                {
                    resultsQueue.Add(product);
                }
                producer2Done = true;
                if (producer1Done && producer2Done) { resultsQueue.CompleteAdding(); }
            });
    
            return resultsQueue.GetConsumingEnumerable();
        }
    
    
        public static IEnumerable<TResult> SelectAsyncUnique<TResult>(this IEnumerable<TResult> source)
        {
            HashSet<TResult> knownResults = new HashSet<TResult>();
            foreach (TResult result in source)
            {
                if (knownResults.Contains(result)) {continue;}
                knownResults.Add(result);
                yield return result;
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say I have two tables orgs and states orgs is (o_ID, state_abbr) and
Let's say I have an facebook application running using the JS SDK. First user
Let say I have two UIViews: View1: - bounds: 0, 0, 20, 20 -
Let's say I have two assemblies: BusinessLogic and Web. BusinessLogic has an application setting
Let's say I have multiple requirements for a password. The first is that the
Let's say I'm building a data access layer for an application. Typically I have
Let's say you have a class called Customer, which contains the following fields: UserName
Let's say we have a simple function defined in a pseudo language. List<Numbers> SortNumbers(List<Numbers>
Let's say I have a drive such as C:\ , and I want to
Let's say that we have an ARGB color: Color argb = Color.FromARGB(127, 69, 12,

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.