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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T23:09:09+00:00 2026-06-01T23:09:09+00:00

We would like to build a pattern around certain loops in our solution, that

  • 0

We would like to build a pattern around certain loops in our solution, that will allow them to run in serial, or parallel, depending on factors. Below is the general form of it.

Since the concurrent collections dont share a common interface with the regular collections, we need some sort of adapter to write general code.

Specifically around the usage of the addFunc delegate in the loop body, is there anything there that would end up causing problems in the long term that we might miss? It runs fine as of now, but….?

Action<SomeType> addFunc;

if(runInParallel)
{
   addFunc = concurrentBag.Add;
   loopDelegate = Parallel.ForEach;
}
else
{
   addFunc = iList.Add;
   loopDelegate = Serial.ForEach; // wrapper delegate for foreach
}

loopDelegate(source, item =>
{
   SomeType result = longRunningTask(item);
   ...
   addFunc(result); // will 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-01T23:09:10+00:00Added an answer on June 1, 2026 at 11:09 pm

    Curious why not use TPL in .NET 4.0?
    http://msdn.microsoft.com/en-us/library/dd537609.aspx

    There is an excellent white paper of the considerations they have taken when developing TPL, if you can’t use .NET 4, you should look at the paper and consider some of the gotcha’s in there.

    Updated based on comment pointing out the obvious.

    I’d use some syntactic sugar like,

    ForEach<Tsource>(Predicate<IEnumerable<TSource>> isParallel, IEnumerable<TSource> source, Action<TSource> body)
    {
        if(isParallel(source))
        {
            Parallel.ForEach<TSource>(source, body);
        }
        else
        {
            foreach (TSource element in source)
            {
                body(element);
            }
        }
    }
    

    There are two main advantages over your implementation.

    1. You enumerate twice, once to add the items to loop over, and a 2nd time during execution.
    2. It is not immediately obvious, but you’re stopping Parallel.ForEach, and foreach from using the most efficient getter. Parallel.ForeEach will not always use GetEnumerator in IEnumerable. An enumerator by design is not concurrent, if your item implements IList, Parallel.ForEach will use the indexer to allow each thread to access the element in the source without waiting on the enumerator to iterate through the list. ConcurrentBag does not implement IList.

    This is the paper I was referring to btw, http://www.microsoft.com/download/en/details.aspx?displaylang=en&id=19222.

    It’s all a very good read if you’re imeplemeting this, but pay particular attention to,
    Pages 1[5-7], 26, 3[0-2].

    The syntactic sugar means you can call it as you would with TPL,

    MyParllelLibrary.ForEach( (list) => true, list), item =>
    {
        // What my code does
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to build a query that will pull the number of rows
how to build a perl regular expresion that would match a pattern like this
I would like to build a cross-platform GUI application in Python that displays PostScript
we have a modularized website built on the MVC pattern. We would like to
I would like to build an excel VBA program to find all the match
I would like to build some code which calls some code on loadup of
I would like to build a static version of QT Library for X11 like
I would like to build some smaller scale but hightly customized documentation sites for
I would like to build a simple animation for a school project. I want
I would like to build a settings interface for the iOS app. Note -

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.